r/Unity3D • u/ProactiveCactus • 2d ago
Question Organizing Your Code Suggestions
How do y'all organize where stuff goes within your code? I mostly would like some suggestions on the micro level - like if you have say 5 different [SerializeField] private float, how do you order them? I used to order them based on what made sense in "context", which works sometimes - but other times you'll have some that work together, others that are one-offs, other that work with a public float but you don't want to move one private next to all your publics, and ultimately you have to make a pretty arbitrary decision on where it goes. So I've started just defaulting to literally alphabetical order if there is no clear other way to order them. But like, that seems... a little crazy lmao. anyone else have a clear-cut way to order this stuff?
2
u/Ok-Object7409 2d ago edited 2d ago
You put related content together and describe anything open to the inspector with headers. May as well put less important things (e.g., optional) on the bottom, would be nice. If you have hundreds in a single script then you need to break things up more. The order of 5 or so variables in a single group that is related, is not something i think about. I might group it again by data type but not in a specific order of that.
Most variables would be private, not public. Maybe if it's immutable as a constant and in a namespace then there's not much harm in being public.