Skip to main content
added 21 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

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; } }
}

JSONUtility can serialize variables, 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 HighScoreEntry.

[Serializable]
private class HighScoreEntry
{
    public int Score;
    public string Name;
}

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 variables 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; } }
}

JSONUtility can serialize fields, 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 HighScoreEntry.

[Serializable]
private class HighScoreEntry
{
    public int Score;
    public string Name;
}

Whether 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, then you have to use getters and setters which encapsulate private fields 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; } }
}
added 860 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

JSONUtility can serialize variables, but it can not serialize properties. TryAs 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 HighScoreEntry and see if it works.

[Serializable]
private class HighScoreEntry
{
    public int Score;
    public string Name;
}

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 variables 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; } }
}

JSONUtility can serialize variables, but it can not serialize properties. Try to get rid of the { get; set; } parts in class HighScoreEntry and see if it works.

JSONUtility can serialize variables, 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 HighScoreEntry.

[Serializable]
private class HighScoreEntry
{
    public int Score;
    public string Name;
}

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 variables 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; } }
}
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

JSONUtility can serialize variables, but it can not serialize properties. Try to get rid of the { get; set; } parts in class HighScoreEntry and see if it works.