There is some structure, for example:

[Serializable] public struct ArrowWalleyStats { public Damage damage; public float radius; public int countOfArrows; public float cooldown; } 

And there is a class that stores an instance of this structure, but, for some reason, stores it as a variable of type object . For example:

 [Serializable] class ArrowWalley { object stats = new ArrowWalleyStats(); } 

When trying to serialize an ArrowWalley class object with the standard Unity class JsonUtility.ToJson(new ArrowWalley()); , the stats field is not serialized.

How can I serialize the stats field? Preferably, using the same standard JsonUtility class.

  • one
    standard Unity Json serializer does not support polymorphism. And to make it easier to understand how it works - just like the Editor works in Unity. All that is displayed in the Editor (class fields in the inspector) will also be serialized in Json - vmchar

1 answer 1

Your stats field is not marked as serializable. Accordingly, the serialization does not occur.
For it to be serialized, you need to either make it public or use the [SerializeField] attribute