- Home /
Serialization Error
I have succeeded in creating a larger error message than any other error message I've ever made. I'm very new to the concept of serialization and have no idea what this error message means. Can anyone decipher this wall of text?
SerializationException: Type UnityEngine.MonoBehaviour in assembly UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null is not marked as serializable.
System.Runtime.Serialization.FormatterServices.GetSerializableMembers (System.Type type, StreamingContext context) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization/FormatterServices.cs:101)
System.Runtime.Serialization.Formatters.Binary.CodeGenerator.GenerateMetadataTypeInternal (System.Type type, StreamingContext context) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/CodeGenerator.cs:78)
System.Runtime.Serialization.Formatters.Binary.CodeGenerator.GenerateMetadataType (System.Type type, StreamingContext context) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/CodeGenerator.cs:64)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.CreateMemberTypeMetadata (System.Type type) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:442)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetObjectData (System.Object obj, System.Runtime.Serialization.Formatters.Binary.TypeMetadata& metadata, System.Object& data) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:430)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject (System.IO.BinaryWriter writer, Int64 id, System.Object obj) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:306)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectInstance (System.IO.BinaryWriter writer, System.Object obj, Boolean isValueObject) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:293)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQueuedObjects (System.IO.BinaryWriter writer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:271)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectGraph (System.IO.BinaryWriter writer, System.Object obj, System.Runtime.Remoting.Messaging.Header[] headers) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:256)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:232)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:211)
Level_Creator.SaveLvl (Int32 s) (at Assets/Scripts/Level_Creator.cs:30)
Level_Creator.Start () (at Assets/Scripts/Level_Creator.cs:22)
For the sake of having everything available the two pieces of code that it is working with are below.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Control_Object : MonoBehaviour {
private GameObject peon;
private int timeGo;
public Control_Object ()
{
}
public Control_Object(int time)
{
timeGo = time;
}
public Control_Object(GameObject pe)
{
peon = pe;
}
public Control_Object(int time, GameObject pe)
{
timeGo = time;
peon = pe;
}
public int getTime()
{
return timeGo;
}
public void setTime(int time)
{
timeGo = time;
}
public GameObject getPeon()
{
return peon;
}
public void setPeon(GameObject pe)
{
peon = pe;
}
}
and
public class Level_Creator : MonoBehaviour {
public static List<Control_Object>[] newLvl = new List<Control_Object>[150];
void Start ()
{
GameObject[] allPeons = GameObject.FindGameObjectsWithTag ("Peon");
for (int j = 0; j < 10; j++) {
newLvl [j] = new List<Control_Object> ();
for (int i = 0; i < (j*10 + 10); i++) {
Control_Object peon = new Control_Object (i * 100 + 100, allPeons [i]);
newLvl [j].Add (peon);
}
}
Debug.Log("Level Created!");
SaveLvl (0);
Debug.Log ("Level Saved!");
}
public static void SaveLvl(int s)
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/realms/realm_" + s.ToString() + ".realm");
bf.Serialize (file, Level_Creator.newLvl);
file.Close ();
}
public static List<Control_Object>[] LoadLvl(int s)
{
if(File.Exists(Application.persistentDataPath + "/realms/realm_" + s.ToString() + ".realm"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/realms/realm_" + s.ToString() + ".realm", FileMode.Open);
List<Control_Object>[] temp = (List<Control_Object>[])bf.Deserialize (file);
file.Close ();
return temp;
}
return null;
}
}
Thanks!
Shooting in the dark here, but $$anonymous$$onoBehaviours are already serializable. Remove [System.Serializable] from Control_Object and see what happens
Thanks for the shot. Removing [System.Serializable] from Control_Object gives me an error saying that Control_Object is not marked as serializable.
Answer by Arkaid · Jul 15, 2016 at 02:06 AM
You're trying to serialize a MonoBehaviour class and that's a no-no, apparently
http://answers.unity3d.com/questions/23734/need-to-serialize-a-monobehaviour-class.html