Search Unity

Serializable fields of serializable class lost after application reload

Discussion in 'Scripting' started by nilsdr, Apr 15, 2021.

  1. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Hello,

    We are having some serialization problems. We have three types, see attached script below. There is a SerializableDictionary that has itself two references to other classes derived off of SerializableDictionary.

    We create an instance of the base dictionary (AdditionalParametersCache) and store it in a ScriptableObject like so:

    Code (CSharp):
    1. [HideInInspector]
    2. [SerializeField]
    3. public AdditionalParametersCache m_AdditionalParametersCache;
    4.  
    Which is then initialised like this:

    Code (CSharp):
    1. m_AdditionalParametersCache = new AdditionalParametersCache();
    2. m_AdditionalParametersCache[typeof(int)] = new IntegerParametersCache();
    3. m_AdditionalParametersCache[typeof(string)] = new StringParametersCache();
    This works fine until we close the editor and then reopen the project. The base dictionary still persists, but its two entries are now null. I tried to read up on how unity handles serialization but can't figure out how this happens. All other serialisable fields of the scriptable object are persisted just fine.

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using System.Security.Cryptography;
    5. using XXX.BuildTools.Misc;
    6. using XXX.BuildTools.SerializableDictionary;
    7.  
    8. namespace XXX.BuildTools.Classes
    9. {
    10.     [Serializable]
    11.     public class AdditionalParametersCache : SerializableDictionary<Type, SerializableDictionaryBase> {};
    12.  
    13.     [Serializable]
    14.     public class IntegerParametersCache : SerializableDictionary<string, int> {};
    15.  
    16.     [Serializable]
    17.     public class StringParametersCache : SerializableDictionary<string, string> {};
    18. }
    19.