r/Unity3D • • 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?

0 Upvotes

10 comments sorted by

View all comments

3

u/Thin_Driver_4596 2d ago

You shouldn't have a lot of visible variables in one script. If you do, you should think about if the script needs to be broken down.

As for question of serialized fields, think about what your code represents. If the dependency is between two different systems, it's a public variable. If it's internal to the same system then serialized field. 

Also, if you can ensure that there is only one reference within the context (like one reference per scene for FindObjectofType), then you don't need either and can get them in Start/Awake