When to use [SerializeField] and why?

I’m kinda new to the concept of serialization - I read about it in the wiki, in stack overflow and in the Unity scripting reference - kinda gave me an idea - but I’m still not sure why and where would I use it in my game.

An example with some explanation would be appreciated - thanks in advance.

3 Likes

I am not completely sure but when I use it, it’s to make private variables visible in the inspector.

For Example:

public int Speed = 10 //This Wil Show In The Inspector
private int Speed = 10 //This Will Not Show In The Inspector
[SerializeField]
private int Speed = 10 //This Will Show In The Inspector
31 Likes

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.

38 Likes

[Serializefield] is just use for showing a private variable’s value on Inspector. You can easily change the value like public variable. But None can access this value from another script or places.

18 Likes

No, you can with the Reflection API.

2 Likes

what is the point of even saying this, for 99% of uses cases if you are using reflection to get private members your approach is flawed.

11 Likes

Hey y’all, sorry for continuing the discussion :stuck_out_tongue: but I just have to ask, what’s the point of using [SerializeField] on a private variable to make it appear on the inspector when you can just make that variable public?

Thanks!

6 Likes

If it’s public any code can change its value, if it’s private only special code like unity deserilizers or other code utilizing reflection.

It’s more robust if external code cant mutate the object

15 Likes

Hold up. If SerializeField is used to make a private field appear on the inspector, why not just change the field to public instead? I am super confused.

3 Likes

Just in case anyone else casts Raise Dead on this thread, restricting access to a variable or method by setting it to private instead of public is primarily done to minimize debugging effort as the fewer things there are accessing a variable or a method the less chances you can introduce a bug.

10 Likes

It’s mainly for teams working on the same stuff, having something private means “don’t modify this value from outside the class” or something alike, I work alone so there’s less chance of that happening, depends on your work environment.

3 Likes

Also, even if you’re not working on a team: you right now vs you in 2 years vs you in 4 years are like completely different people.

If you write some kind of app or game as a solo programmer and it isn’t a throwaway project that you just write once and then forget about, then you will be presumably debugging and refactoring and extending it for the next 2-4 years. Future versions of you will appreciate proper API design and encapsulation done right the first time.

9 Likes

Entertaining thread : )

6 Likes

Please don’t. Necroposting is incredibly frustrating.

8 Likes