Every time you attach a
MonoBehaviour script to a game object, all the public variables in that script are automatically exposed in the inspector. This means you can change the values directly from there and also these values are serialized.
Serialization is the process of converting an object into a stream of bytes in order 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 to be able to recreate it when needed.
Making your variables public is not the only way to expose them in the Inspector. An alternative is using the
SerializeField attribute, like in the
Level class variables, so independent of the access modifier of the property (public, private, protected, or internal), this will be exposed and serialized without exception.
In specific scenarios, you might want to have a public property to be serialized without exposing it in the inspector. For this, you must use the