- Home /
Serial Exemption when saving binary file
I'm trying to save two elements, a string and a list of strings to a binary file. I have the class set outside the main script:
}
the main script
}
[Serializable]
class SaveManager
{
public string Info;
public List<InfoSheetList> InfoSheetList ;
}
Then the function that calls it all
public void GetInfoSheet()
{
DatabaseManager.Instance.SQLiteInit();
string Info = PlayerPrefs.GetString ("Info");
string date = PlayerPrefs.GetString ("Date");
string Date=date.Replace("/","-");
string Name = PlayerPrefs.GetString ("Name");
string FilePath="C:/InfoSheets";
DatabaseManager.Instance.SQLiteInit();
InfoSheetList.Clear();
InfoSheetList= DatabaseManager.Instance.MakeInfoSheetList();
Debug.Log("How many in the list " + InfoSheetList.Count);
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = File.Create(FilePath + "/" +Date+Name+"InfoSheet.gig");
Debug.Log (fs);
SaveManager sm= new SaveManager();
sm.Info=Info;
sm.InfoSheetList=InfoSheetList;
//PlayerPrefs.DeleteAll ();
bf.Serialize(fs, sm);
fs.Close();
Debug.Log(sm+ "Saved!");
Only I'm getting an exception- SerializationException: Type 'InfoSheet' in Assembly 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers (System.RuntimeType type) (at :0) System.Runtime.Serialization.FormatterServices+<>c_DisplayClass9_0.b0 (System.Runtime.Serialization.MemberHolder ) (at :0) System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) (at :0) System.Runtime.Serialization.FormatterServices.GetSerializableMembers (System.Type type, System.Runtime.Serialization.StreamingContext context) (at :0) System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo () (at :0) System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize (System.Type objectType, 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.SerializationBinder binder) (at :0) System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize (System.Type objectType, 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.SerializationBinder binder) (at :0) System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray (System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo objectInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo memberNameInfo, System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo memberObjectInfo) (at :0) System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write (System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo objectInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo memberNameInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo typeNameInfo) (at :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 :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 :0) System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) (at :0) System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) (at :0) GigSheetManager.GetGigSheet () (at Assets/Scripts/GigSheetManager.cs:210) UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()
What am I missing. The string is serializing, but the list is not. Thanks in advance!
Answer by WeirdBeardDev · Aug 18, 2020 at 05:29 PM
The type InfoSheet
is not serializable, add the Serializable
attribute to the class and see if that fixes it.
I added System.Serializable to the Infosheet class and it worked! Thanks!
Your answer
Follow this Question
Related Questions
[Serialization] Multiple classes into one file? 1 Answer
C# how to setup a Binary Serialization 4 Answers
Multiple Cars not working 1 Answer
c# - Function saved through Binary, then XML, then loaded 1 Answer
Serializing an Action as XML? 0 Answers