- Home /
UnityEngine.GameObject not marked as Serializable?
I'm getting this error, but I thought that all types derived from UnityEngine.Object were automatically serializable if made public, or using the [SerializeField] attribute?
Am I missing something?
Answer by Wolfram · Jun 06, 2012 at 07:58 PM
Are you trying to use your own serialization interface? Google your question, there are several answers, threads, and other topics dealing with problems when trying to do that.
Other than that, my wild guess would be, don't use [SerializeField] or other related attributes above objects that are already serialized by Unity, include a "using UnityEngine;" instead of accessing it via UnityEngine.GameObject, etc.
The serialization is an effort to do a deep copy using binary formatter and memory stream. It works perfect if the item I'm deep copying doesn't contain a reference to a GameObject. When it does, it throws the error that the GameObject is not marked as serializable.
Unity doesn't use the C# serializer. They uses their own serialization method. $$anonymous$$ost UnityEngine classes have a counterpart in native code. They are more than the managed representation you can see from $$anonymous$$ono. Don't mess around with lowlevel stuff when it comes to build in classes of Unity.
They actually sealed all classes so you don't accidentally derive a class from a Unity class. The only classes that can and should be used as base class is $$anonymous$$onoBehaviour and ScriptableObject (and the editor classes that are ment to be derived like Editor, EditorWindow, AssetPostprocessor, ...)
If you want to deep copy a UnityEngine.Object derived object, use Instantiate. It works with most objects (i used it already for $$anonymous$$aterials, $$anonymous$$eshes and of course gameobject and components). $$anonymous$$eep in $$anonymous$$d when you clone a component the owning gameobject is cloned as well along with all components and child objects.