JSONUtility can serialize variablesfields, but it can not serialize properties. As the documentation of [SerializeField] says:
The serialization system can do the following:
- CAN serialize public non-static fields (of serializable types)
- CAN serialize nonpublic non-static fields marked with the SerializeField attribute.
- CANNOT serialize static fields.
- CANNOT serialize properties.
So how do we solve this problem? The most simple solution would be to get rid of the { get; set; } parts in class HighScoreEntryclass HighScoreEntry.
[Serializable]
private class HighScoreEntry
{
public int Score;
public string Name;
}
IfWhether those fields are raw public fields or properties with trivial getters and setters shouldn't really change anything in this particular case. But if you really want to use properties in a serializable object for some reason (there is no reason in this example because both setters and getters are trivial, but perhaps you have a more complex use-case in mind), then you have to use getters and setters which encapsulate private variablesfields marked with the [SerializeField] attribute:
[Serializable]
private class HighScoreEntry
{
[SerializeField] private int score;
public int Score { get { return this.score; }
set { this.score = value; } }
[SerializeField] private string name;
public int Name { get { return this.name; }
set { this.name = value; } }
}