Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use SerializeField?

Tags:

c#

unity3d

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?

like image 371
Eminent Emperor Penguin Avatar asked Nov 07 '18 14:11

Eminent Emperor Penguin


People also ask

What does serialize mean programming?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed.

Should I use SerializeField or public?

In Unity, public fields are displayed in the Inspector. Public fields are serializable by default, just like the areas you mark with SerializeField. You might prefer SerializeField rather than public because you may want to see your variable in the Inspector but keep it non-accessible.

What is the point of SerializeField?

If you also want Unity to serialize your private fields you can add the SerializeField attribute to those fields. Unity serializes all your script components, reloads the new assemblies, and recreates your script components from the serialized versions.

What does serializing do in Unity?

Serialization is the automatic process of transforming data structures or object states into a format that Unity can store and reconstruct later. Some of Unity's built-in features use serialization; features such as saving and loading, the Inspector window, instantiation, and Prefabs.


1 Answers

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.

like image 88
Programmer Avatar answered Sep 17 '22 19:09

Programmer