is SerializeField a bad habit?

I got used to declare variables with the [SerializeField] code, because for me its easier to assign values to the variables in the editor inspector than by code. But i don’t know if this is a god habit at all. I want to know if using SerializeField can provoke any problems to me. Thanks :slight_smile:

It’s much better to use [SerializeField] than making the field public so I’d say you’re on the right track.

Serializing private fields is fine and doesn’t hurt anything in your game so no worries.

Thinking about it the only potential (functionality) downside to serializing private fields is on Scriptable objects. When you use a scriptable object in game, the private fields tagged with serialize attribute won’t get reset after you leave play mode.

as for habbits, it really depends on your coding style. One downside on that front that I can think of is that marking a field with serializeField attribute makes you want to not include the functionality to set the variable through code and that means bye bye constructors.

[SerializedField] private... is a GREAT habit. It is how Unity is designed to work – by enforcing good programming practices with encapsulation but still allowing human developers to easily make changes without requiring recompilation of the codebase to tinker with values.

Keep up the good practices!

I use public instead of [SerializeField] private without any issue. I think public is better as it is more concise and also allows me to read things from other places without the hassle of getters/setters.

Some people will talk about the benefits of “separation of concerns” and “encapsulation”. Just try to be pragmatic on whatever your choices are. I am not a fan of following a “correct way” if the “wrong way” is easier, and in practice nothing of the “issues” are an issue at all.