54

I have just started to learn C# and Unity, and there is one thing that I can not get used to:

Why and when should I use [SerializeField]?

Is it bad to leave variables hard coded despite using [SerializeField] and have more text boxes in my unity interface?

Francesco - FL
  • 603
  • 1
  • 4
  • 25

2 Answers2

75

Why and when should I use [SerializeField]?

Using the SerializeField attribute causes Unity to serialize any private variable. This doesn't apply to static variables and properties in C#.

You use the SerializeField attribute when you need your variable to be private but also want it to show up in the Editor.

For example, this wouldn't show up in the Editor:

private float score;

And this is because it's a private variable but the one below should show up in the Editor:

[SerializeField]
private float score;

That's because you applied SerializeField to it and you're telling Unity to serialize it and show it in the Editor.


Note that private variables has more to do with C# than Unity. There is also public variable variables. Marking your variable private means that you don't want another script to be able to access that variable. There is also public qualifier. Marking your variable public means that you want your other scripts to be able to access that variable.

Sometimes, you want other scripts to be able to access your variable from another script but you don't want the public variable to show up in the Editor. You can hide the public variable with the [HideInInspector] attribute.

This will show in the Editor:

public float score;

This will not show in the Editor:

[HideInInspector]
public float score;

Is it bad to leave variables hard coded despite using [SerializeField] and have more text boxes in my unity interface?

Yes, it's mostly bad especially for new users. It shouldn't be a big deal for a long time Unity and C# programmer. The reason this is bad is because when you have the code below:

[SerializeField]
private float score = 5f;

The default value is 5 in the Editor. Once you save the script this variable is now updated in the Editor as 5. The problem is that you can change this from the Editor to 14. Once you change it from the Editor, the value in the script will still be 5 but when you run it, Unity will use the value you set in the Editor which is 14. This can cause you so much time troubleshooting something that isn't even a problem just because there is a different value being used that is set in the Editor while you're expecting the default value set in the script to be used.

The only way for for the score variable to reset back to it's default 5 variable is when you either rename the variable to something else or reset it from the Editor. It won't even change even when you change the value from 5 to 3 from the script. It has to be renamed or reset from the Editor. It's worth knowing but when you get used to Unity, you won't have to worry about this.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • It is also worth noting that `SerializeField` not only shows that value in the Inspector but more importantly also makes it be serialized => saved (as [Draco18s mentioned](https://stackoverflow.com/a/53192519/7111561)) – derHugo Nov 07 '18 at 17:25
  • @derHugo I wouldn't really use the work "save" to describe it. Even the [doc](https://docs.unity3d.com/ScriptReference/SerializeField.html) did not. It would make it look like once the variable is marked with `[SerializeField]`, changing it during run-time via code, it will save itself which is not quite true. I simply mentioned that when `public` or marked as `[SerializeField]`, the new value is in the Editor and that overrides the one in the code and will never change unless it is renamed or reset – Programmer Nov 07 '18 at 17:37
  • No the word is "serialized". What I mean is a usual `private int` will e.g. not be saved meaning it won't appear e.g. in the unity scene files after saving them. You might e.g. set a private value from an editor script but if it is not serialized it will be useless and always have it's default value. – derHugo Nov 07 '18 at 17:46
  • @derHugo Can you please elaborate on this explanation. I have just started unity and this is very confusing to me. Everywhere I look, the definitions say it makes some thing "serializable" or "it is serialized...". What does that actually mean? – Ak01 Oct 04 '20 at 14:08
  • 1
    @AyushKoul checkout [Unity Manual - Script Serialization](https://docs.unity3d.com/Manual/script-Serialization.html) this should explain it. In short basically: Serialization = Convert c# instances and values into a file format => stored. Deserialization = Read such a file => convert the file content into c# instances, values etc. So basically the file `Example.unity` is the serialized file containing all information you see in your Example scene in Unity ;) – derHugo Oct 05 '20 at 07:51
  • I see. But does this mean that if I change a serialized variable in the game via code, it will be saved to? Or does it only apply to things that I change manually in the inspector? – Ak01 Aug 25 '21 at 20:25
  • Is it specific to private serialized fields that Unity only uses the value set in the editor? What if I have a public field (that's serialized by default) — will it use the value I specify in the script or in the editor, if the field is public? – Matt Jun 10 '23 at 17:21
8

The [SerializeField] attribute is used to mark non-public fields as serializable: so that Unity can save and load those values (all public fields are serialized by default) even though they are not public.

That's all it does. You use it when you want that.

  • 1
    It also makes that variable visible/adjustable in the Inspector. – derHugo Nov 07 '18 at 17:26
  • You can do that by setting the inspector to `debug` as well. So I wouldn't suggest using it in order to inspect values. – Draco18s no longer trusts SE Nov 07 '18 at 18:46
  • 2
    Yes but you might want to change/set the value of private variables. That's what I meant – derHugo Nov 07 '18 at 19:06
  • Can someone explain what serialization is? Even in the definitions, it says it makes some thing "serializable" or "it is serialized...". What does that actually mean? – Ak01 Oct 04 '20 at 14:07
  • 2
    @AyushKoul Sometimes you might want to dump all your class data into a file or a string, like a JSON or a binary, so you can afterwards load it from such file (maybe because you did a save file that you want to load in the future, for example). Serialization/Deserialization is the act of converting from your raw data to some arbitrary format and back. See https://docs.unity3d.com/Manual/JSONSerialization.html – Dani Barca Casafont Oct 22 '20 at 14:38