1

I have class like -

[serializable]
public class DemoClass{
    [SerializeField]
    public GameObject gameObject;
}

I want to store data using serializable. Unity doc says GameObject is serializeField. But it gives Exception -

SerializationException: Type UnityEngine.GameObject is not marked as Serializable.

Anyone have idea about it ?

2 Answers2

1

Add the tag [Serializable] before your class name:

[Serializable]
public class DemoClass{
    public GameObject gameObject;
    public AudioClip audio;
}

Documentation

Edit: ff you still want to serialize GameObject their is some plugins out there that allow to Serialize Unity Objects. See: Runtime Serialization for Unity

Ludovic Feltz
  • 11,416
  • 4
  • 47
  • 63
  • I use this. But Gameobject is not allow to serialize it gives Exception. – Ankita Korape May 24 '17 at 09:15
  • @AnkitaKorape Why do you want to serialize a `GameObject` ? – Ludovic Feltz May 24 '17 at 09:35
  • I have many assetBundles. When first time i download it using www, i don't want to download/cache it again.Because it occupies so much of phone memory and app gets crashes.So I want to save data for further use. – Ankita Korape May 24 '17 at 09:40
  • @AnkitaKorape Ok have a look at my edit it should solve your issue – Ludovic Feltz May 24 '17 at 09:41
  • Can someone explain when a class has to be serializable? I looked in the documentation and it says to apply it when it is serializable? Oh, and make it laymen's terms please. – Chad May 24 '17 at 11:51
0

You will need to add the [serializable] attribute to the classes that you wish to serialize. If any of the classes within your class are not already serializable, you will need to add this attribute to those classes as well.

e.g.

[Serializable]
public class DemoClass
{
   public GameObject gameObject;
   public AudioClip audio;
}
Neil
  • 11,059
  • 3
  • 31
  • 56