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

Scriptable Object Serialization Change for 2019.1.5f1

Discussion in 'Scripting' started by alexisrabadan, Jun 10, 2019.

  1. alexisrabadan

    alexisrabadan

    Joined:
    Aug 26, 2014
    Posts:
    82
    Has there been a change to the way scriptable objects are serialized?

    In the Awake method for one of our scripts, we use the AssetDatabase to find similar objects using LoadAssetAtPath. This worked fine for the past 3-4 years, but now is causing StackOverflowExceptions when updating our project to 2019.

    The overflow occurs due to Awake being called via the LoadAssetAtPath method (which in turn runs the Awake & LoadAsset again).

    We also have an Application.isPlaying check in the Awake method which throws an error stating that the check should not be done in a serialization method.

    What has changed that seems to break everything?

    Code (CSharp):
    1.         void Awake()
    2.         {
    3. #if UNITY_EDITOR
    4.             List<Key> allKeys = FindAllKeyAssets();
    5.  
    6.             if (id == 0)
    7.             {
    8.                 id = Random.Range(0, int.MaxValue).GetHashCode();
    9.                 Debug.Log("Set ID for " + name);
    10.             }
    11.  
    12.             foreach (Key key in allKeys)
    13.                 if (key != this && key.id == id)
    14.                 {
    15.                     id = 0;
    16.                     Debug.Log(name + " has the same ID as " + key.name);
    17.                     Awake();
    18.                 }
    19. #endif
    20.         }
    21.  
    22.         private List<Key> FindAllKeyAssets()
    23.         {
    24.             List<Key> assets = new List<Key>();
    25.             string[] guids = AssetDatabase.FindAssets(string.Format("t:{0}", typeof(Key)));
    26.             for (int i = 0; i < guids.Length; i++)
    27.             {
    28.                 string assetPath = AssetDatabase.GUIDToAssetPath(guids[i]);
    29.                 Key asset = AssetDatabase.LoadAssetAtPath<Key>(assetPath);
    30.                 if (asset != null)
    31.                 {
    32.                     assets.Add(asset);
    33.                 }
    34.             }
    35.             return assets;
    36.         }