So I have two c# classes (Team and Player) and I want to store a team into a json file. Every team has some players. I think it should look like this:
{
"name":"Chelsea"
"players": [
{
"name":"Player1"
},
{
"name":"Player2"
},
{
"name":"Player3"
}
]
}
But sadly it isn't working for me. It always only returns:
{"name":"Chelsea"}
I do not really know why this happens. Is it even possible to do this? My code is:
public class Team
{
public string name = "testTeam";
public Player[] players = new Player[0];
public void AddPlayer(Player player)
{
int i = players.Length;
players = new Player[i+1];
players[i] = player;
}
public void save()
{
File.WriteAllText(Application.dataPath + "/data/teams/" + name + ".json", JsonUtility.ToJson(this));
}
}
public class Player
{
public string name;
}