Question by
hellowhatsup1982 · Dec 21, 2016 at 08:46 PM ·
c#instanceuser
Saving more than one class instance to JSON
[System.Serializable]
public class User
{
public float HighestScore;
public string Name;
public static int UserCount { get; private set; }
public User(string name,float highestScore)
{
this.Name = name;
this.HighestScore = highestScore;
UserCount += 1;
}
public string ToJsonString()
{
return JsonUtility.ToJson(this);
}
public void PrintUserCount()
{
Debug.Log(UserCount);
}
}
I managed to save only one user using JsonUtility but I want to save a couple of users to the same json file. How can I do that?
Comment
Answer by Landern · Dec 22, 2016 at 03:19 PM
List<User> m_Users = new List<User>();
In some method
JsonUtility.ToJson(m_Users);
Semantically speaking, your User type/class would convey a single user, an array or List of User type should hold from 0 to n amount of Users, doing a m_Users.Count will return the amount of users and you will not need to track that variable internally since User conveys the thought of a single user rather then users.
Your answer
![](https://koobas.hobune.stream/wayback/20220612103211im_/https://answers.unity.com/themes/thub/images/avi.jpg)