What does [System.Serializable] do. I have a nested class inside my monobehaviour and i get errors when i take away [System.Serializable].
Unfortunately the most lucid explanation I have found involved sock puppets and a laser pointer.
However I know that if you nest classes, they all gotta be marked as serializable, like turtles all the way down, and your fields have to be public, and they cannot be properties.
From the documentation:
Often in Unity it is used so things can appear in the Inspector. The Unity Inspector generally can only display fields which are serializable. Otherwise the Inspector will only display fields of basic and Unity types which are set public.
Without seeing the code I can’t say exactly why removing it would cause errors. (In my experience, removing it usually just causes things to silently fail to save)
But in general, Serializable basically means, you can serialize it - meaning, you can save the data in this class to some kind of stream or string. This is useful in quite a few scenarios, most of which involve saving data in some way or another.
- The Unity Inspector can only show you fields that are serializable. MonoBehaviour itself is already serializable, but any custom classes that you want to show up in the inspector must be as well.
- Naturally this is also how Unity goes about saving your script’s data into scene files and prefabs.
- File saving. If you want to use BinaryFormatter to save a class’s data to file, it’ll use Serializable. Same with JsonUtility, Json.NET, LitJson, and anything else.
There are more, but these are the most common uses for it.