0

I'm struggling with so simple thing developing game in Unity.

I mark my struct as Serializable. This struct has several fields, including array of custom serializable classes.

[Serializable]
public struct GameSerialized {
    public StorageSerialized storage;
    public BankSerialized bank;
    public object[] buildings;
}

After inspecting JSON output JsonUtility.ToJson(new GameSerialized(game)) unfortunately there is nothing about buildings. Buildings array contains only serializable structs, i.g. HouseSerialized, MillSerialized etc. I think it is the problem of declaration object[], because if I declare it e.g. public HouseSerialized[] houses they are very nicely serialized.

I'd like to use pure language functionality, trying to avoid using custom libs. And I want to do proper architecture, so that game doesn't really know what buildings are inside, as long as they can be serialized.

Thanks for your help.

Chris Rutkowski
  • 1,774
  • 1
  • 26
  • 36
  • Your title is misleading as this has nothing to do with array. Unity's `JsonUtility` cannot serialize `object`. You have to explicitly mention what type of building is. – Programmer Oct 15 '17 at 14:16
  • *"I'd like to use pure language functionality, trying to avoid using custom libs"* You can't. You will run into issues especially on iOS. There are two options for you: Use `enum` to store the type of building instead of `object` or use one of the third party libraries(Newtonsoft or SimpleJSON) from the duplicate which supports `object`. – Programmer Oct 15 '17 at 14:18

0 Answers0