I’ve come across an issue with my save script, which probably has to do with my lack of understanding of serialization.
My class has a List that gets populated at runtime when a new player is created. However, saving causes an error (shortened for brevity):
[System.Serializable]
public class PlayerStats {
// ...
public List<SkillTree> trees = new List<SkillTree>();
// ...
// Use this for initialization
public PlayerStats() {
initSkillTrees();
}
// ....
private void initSkillTrees() {
// create skill tree etc
trees.Add(newlyCreatedSkillTreeObj);
}
And I receive this error:
SerializationException: Type PlayerStats+<initSkillTrees>c__AnonStorey4E is not marked as Serializable.
System.Runtime.Serialization.Formatters.Binary.BinaryCommon.CheckSerializable (System.Type type, ISurrogateSelector selector, StreamingContext context) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryCommon.cs:119)
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:386)
Which is confusing to me, since I thought serializing was on the fields and not the methods, but it is specifically telling me that the method is having a problem.
The SkillTree class also references a Skill class below it, but every class is marked as [System.Serializable].