Serializefield vs Public, Will This Cause Problems Later?
So I Was Looking Back on Some of My Stuff Here, and I Noticed That I Was Using a Lot of [Serializefield], Instead of Public. I Like to Do This So I Can See...
So I was looking back on some of my stuff here, and I noticed that I was using a lot of [SerializeField], instead of public. I like to do this so I can see exactly what's what when I actually start testing this stuff out. However, I haven't gotten very far in my project, and I'm still learning as I go, but was wondering if this overuse of [SerializeField] might cause problems later. So, my question is: Will using serialize field instead of public cause any problems if I use it a lot?
2 Answers
tl;dr: For your purpose don't use either but rather enable the Inspector -> Debug Mode (bottom of the page)! (Read more below in the conclusion)
Serialization
First of all I think you should read about Unity Script Serialization in general.
(De)Serialization is basically any process converting between a runtime type instance and permanent memory such as binary or text files.
Purpose of Serialization in Unity
In Unity the hole main purpose of serialization is saving values and references in assets such as Scenes, Prefabs, ScriptableObjects, etc to YAML (somewhat similar to JSON).
These asset classes/files can be saved and loaded meaning on runtime they get recreated with the saved values and references. Now while this seems obvious and we take it for granted usually, there are frameworks (e.g. OpenSceneGraph) where you literally have to compose and configure the entire scene via native code from scratch everytime the application is started.
The other purpose enabled by the before is being able to expose and adjust serialized fields via the Inspector for each instance individually and have different instances of the same type with different field values.
Example: Imagine having two Enemy instances but one should run with speed 3 the other one with 1.5. By exposing and (de)serializing this field you can have one single class for both and simply set and store different values for the same field without having to implement this in your code itself.
SerializeField
By default (see link above) Unity (like most serializers) only serializes public fields. Most of the time this makes sense since the private fields usually are things that are only relevant on runtime for storing temporary values.
However, sometimes you would not want to expose a field public so each and every other class could access and change that field value, so for keeping encapsulation you would want to keep certain fields private or protected. But you still want to serialize them for the afore mentioned reasons: Being able to adjust them via the Inspector and save them persistent.
This is where the purpose of [SerializeField] kicks in. It allows you to force serialization also of non-public fields.
And then there is a not documented second use case of [SerializeField]: It allows you to serialize Properties which generally are completely ignored by Unity's Serializer!
[field: SerializeField] public int SomeProperty { get; private set; }
will force the Serializer to treat this property as if it would be a field which is extremely useful again for the encapsulation.
Conclusion
For Unity's Serializer it makes no difference whether a field now is public or [SerializeField] private, in both cases it uses something similar to Reflection and accesses the fields by their names so there is no performance gain in making them public.
BUT for your specific case
I like to do this so I can see exactly what's what when I actually start testing this stuff out.
you really should avoid using [SerializeField] as you are not really wanting to serialize these! This of course has an impact on performance and might lead to unexpected results in case you changed such a value vis the Inspector. Because in that case the Serializer will deserialize and overrule whatever later change on the default value of the fields! I'd recommend/present to you the Inspector -> Debug Mode (bottom of the page)!
Normally, the Inspector window is configured as an editor for the selection’s properties. But sometimes it’s useful to only see the properties and their values. When you activate Debug mode, the Inspector shows only the properties and their values. If the selection has script components, Debug mode also displays private variables, although you can’t edit their values.
You can toggle Debug mode for each Inspector window individually.
- To turn on Debug mode, click the More Items (⋮) button to open the context menu, and select
Debug.- To return to Normal mode, click the More Items (⋮) button to open the context menu, and select
Normal.
Serialization is the process of taking an object in ram (classes, fields, etc...) and making a disk representation of it which can be recreated at any point in the future. When you apply the SerializeField attribute to a field, it tells the unity engine to save/restore it's state to/from disk. You mostly use serialization for the editor, and especially when building your own editor windows and inspectors.
For most games (especially simple ones) PlayerPrefs is sufficient for saving state to disk.
Note: this was something I found on google
My own note, if you really not going to use your own inspector then using SerializeField may decrease some of the performance.