Search code examples
c#unity-game-engineserializationgameobjectaudioclip

Serialize GameObject in Unity


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 ?


Solution

  • 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