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

Bug ScriptableObjects referenced in Addressable scenes references lost

Discussion in 'Addressables' started by glitchers, Apr 7, 2021.

  1. glitchers

    glitchers

    Joined:
    Apr 29, 2014
    Posts:
    64
    I am using ScriptableObjects to store data/state between scenes and my current workaround for each reference to the ScriptableObject in any Addressable Group is that every object I use in this way has a GUID. Then before I use the ScriptableObject I check a global/static dictionary which uses the GUID as the key and a ScriptableObject as the value.

    E.g. Object A has a GUID of "12345". Its referenced in Scene1 and Scene2 (both Addressable scenes) where in each scene they have different InstanceIDs (because they are copies). I use the GUID of Object A to check if anything with that GUID exists in my dictionary, if not I add that instance, if it does, I'll return the copy of Object A in the dictionary.

    This has been working well for most use cases and aside from having to run this short method before I use a potential copy it's no hassle.

    However I have found that in the case of the only serialized reference for the ScriptableObject being in 1 Scene which I then later unload I can see the values in the dictionary go to null. I can see OnDestroy is not being called on those objects as I have that log an error.

    I am already setting the HideFlags.DontUnloadUnusedAsset on the object as well as DontDestroyOnLoad for good measure. I have tried also making the static dictionary into a non-static one which is stored on a Singleton instance Monobehaviour. I have also tried adding a serializable list which stores an additional reference but the references are still being lost.

    This does not happen with objects referenced outside of that scene, e.g. ones also referenced in the root scene. Only ones serialized only by the scene. But then HideFlags.DontUnloadUnusedAsset and/or the external static/DontDestroyOnLoad references should keep the reference count high enough to not unload the assets.
     
  2. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,816
    We'll flag for the team to have a look. Which version of Addressables are you using?
     
  3. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    339
    Hi, version 1.20.3, the problem is still here :/
     
    Last edited: Jul 14, 2022