Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Saving Serialization with Lists

Discussion in 'Scripting' started by SpeakUpGames, Nov 22, 2016.

  1. SpeakUpGames

    SpeakUpGames

    Joined:
    Nov 25, 2014
    Posts:
    56
    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<SkillTree> that gets populated at runtime when a new player is created. However, saving causes an error (shortened for brevity):

    Code (CSharp):
    1. [System.Serializable]
    2. public class PlayerStats {
    3.  
    4.        // ...
    5.  
    6.     public List<SkillTree> trees = new List<SkillTree>();
    7.  
    8.        // ...
    9.  
    10.     // Use this for initialization
    11.     public PlayerStats() {
    12.        
    13.       initSkillTrees();
    14.     }      
    15.  
    16.     // ....
    17.  
    18.     private void initSkillTrees() {
    19.        // create skill tree etc
    20.        trees.Add(newlyCreatedSkillTreeObj);
    21.     }
    22.  
    And I receive this error:

    Code (CSharp):
    1. SerializationException: Type PlayerStats+<initSkillTrees>c__AnonStorey4E is not marked as Serializable.
    2. 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)
    3. 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].
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    Looks like some field inside of your serialized object graph is not marked as Serializable.
     
  3. SpeakUpGames

    SpeakUpGames

    Joined:
    Nov 25, 2014
    Posts:
    56
    Thanks, yes this was the issue. It wasn't a list problem, but a problem with Actions.
    I was using a Skill class with an Action field, which gets populated in the method. Stuff in there wasn't serializable. I will have to re-work this a bit.