Use [SerializeField] and [Serializable] properties to control class serialization

Unity: Use [SerializeField] and [Serializable] properties to control class serialization


By default Unity serializes supported public fields and ignores all private fields. You can control that behavior using [SerializeField] and [Serializable] attributes.
[SerializeField]
private int accumulator; // it will be serialized too!

You can also do the opposite – having a public field, but its value shouldn’t be stored.
[NonSerialized]
public float currentHealth;

If you want to create a custom inner type (inside another class) and serialize it inside it, you must add to it a [Serializable] attribute.

class World : MonoBehaviour {
 public Bot[] bots;
 
 [Serializable]
 public class Bot {
  public string name;
  public int health = 100;
 
  [SerializeField] // you can use it here too!
  private int somethingHidden = 5;
 }
}

Please refer to the Unity3D documentation to read more about [SerializeField], its limitation, and possibilities.