- Home /
The question is answered, right answer was accepted
Can someone please help with this file saving error?
SerializationException: Type 'UnityEngine.MonoBehaviour' in Assembly 'UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers (System.RuntimeType type) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
System.Runtime.Serialization.FormatterServices+<>c__DisplayClass9_0.b__0 (System.Runtime.Serialization.MemberHolder _) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
System.Runtime.Serialization.FormatterServices.GetSerializableMembers (System.Type type, System.Runtime.Serialization.StreamingContext context) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo () (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize (System.Object obj, System.Runtime.Serialization.ISurrogateSelector surrogateSelector, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.Formatters.Binary.SerObjectInfoInit serObjectInfoInit, System.Runtime.Serialization.IFormatterConverter converter, System.Runtime.Serialization.Formatters.Binary.ObjectWriter objectWriter, System.Runtime.Serialization.SerializationBinder binder) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize (System.Object obj, System.Runtime.Serialization.ISurrogateSelector surrogateSelector, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.Formatters.Binary.SerObjectInfoInit serObjectInfoInit, System.Runtime.Serialization.IFormatterConverter converter, System.Runtime.Serialization.Formatters.Binary.ObjectWriter objectWriter, System.Runtime.Serialization.SerializationBinder binder) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize (System.Object graph, System.Runtime.Remoting.Messaging.Header[] inHeaders, System.Runtime.Serialization.Formatters.Binary.__BinaryWriter serWriter, System.Boolean fCheck) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers, System.Boolean fCheck) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
Here's my "SaveSystem":
using System.IO;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
[System.Serializable]
public static class SaveSystem
{
public static void SavePlayer (BallLaunch saver)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.golfthingy";
FileStream stream = new FileStream(path, File$$anonymous$$ode.Create);
LevelCompleted data = new LevelCompleted(saver);
formatter.Serialize(stream, data);
stream.Close();
}
public static LevelCompleted LoadPlayer()
{
string path = Application.persistentDataPath + "/player.golfthingy";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, File$$anonymous$$ode.Open);
LevelCompleted data = formatter.Deserialize(stream) as LevelCompleted;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in " + path);
return null;
}
}
}
Answer by Pangamini · May 13, 2019 at 09:40 AM
I don't see the 'LevelCompleted' class in your source, but I will assume that it derives from MonoBehaviour? Anyway the exception above says that you are trying to use the .net serializer to serialize a MonoBehaviour, which is not allowed ('not marked as serializable' part). Also, the deserialization would be tricky, as you shgould not call its constructor directly (or from the deserializer), but call AddComponent() instead. Try to create the monoBehaviour externally, and just fill it with deserialized data. You can also write surrogates for the serializer, a way to provide a custom handling code for the unknown / non-serializable classes. See the .net docs for the serialization.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Flip over an object (smooth transition) 3 Answers
VBScript through C# in Unity 0 Answers
Write and read text files with c# 1 Answer