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.
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.
[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.
Hey y’all, sorry for continuing the discussion 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?
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.
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.
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.
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.