Inspector variables from class in List are always initialized to its default values

When I want to make a public container (e.g. Array/List) of a class, I always want that the values of the class be the same as I declared in the script.

Of course, I can edit those variables in the inspector to match them again, but it is just annoying setting all the variables back to the values of the declaring section from the class.


For Example:

public class TeamManager : MonoBehaviour {

    [System.Serializable]
	public class Team {
		public string name = "NEUTRAL";
		public Color color = Color.white;
		public Sprite icon;
		public int unitsLeft = 100;
		public List<MainAgent> agentsPrefabs;
		public List<MainAgent> activeAgents;
	}

	public List<TeamManager.Team> teams;
}

The result is that “color” is cleared, “name” is an empty string and “unitsLeft” is 0.

Create a constructor that defaults to those values inside that class.

[System.Serializable]
    public class Team {
        public string name = "TEAMNAME";
        public Color color = Color.white;
        public int unitsLeft = 100;

        public Team()
        {
            name = "TEAMNAME";
            color = Color.white;
            unitsLeft = 100;
        }   
    }

    public List<Team> teams;

    void Awake()
    {
        teams = new List<Team>();
        teams.Add(new Team());
    }

Whenever you want to add a new team, say when a player clicks a button simply use
teams.Add(new Team());