Search Unity

Addressables and DontDestroyOnLoad doesn't work

Discussion in 'Addressables' started by gw1108, Nov 25, 2018.

  1. gw1108

    gw1108

    Joined:
    Jan 22, 2013
    Posts:
    6
    If you Instantiate an addressable and mark it DontDestroyOnLoad, it will be destroyed on load anyways. As if it wasn't marked properly. The scene name of the addressable and moving the addressable also do not matter. Using Resources.Load will function as normal.

    Example code. Requires an addressable named "Prefab1" and two scenes Sample1 and Sample2. Sample1 scene needs a Global gameobject with this monobehaviour attached to it. You should notice that Prefab1 will be in DontDestroyOnLoad but then disappear, presumably because the scene changed.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.AddressableAssets;
    4.  
    5. public class Global : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         DontDestroyOnLoad(gameObject);
    10.         var async = Addressables.Instantiate<GameObject>("Prefab1");
    11.         async.Completed += Async_Completed;
    12.     }
    13.  
    14.     private void Async_Completed(UnityEngine.ResourceManagement.IAsyncOperation<GameObject> obj)
    15.     {
    16.         DontDestroyOnLoad(obj.Result);
    17.         StartCoroutine(NextScene());
    18.     }
    19.  
    20.     private IEnumerator NextScene()
    21.     {
    22.         yield return new WaitForSeconds(1f);
    23.         UnityEngine.SceneManagement.SceneManager.LoadScene("Sample2");
    24.     }
    25. }
    26.  
     
  2. I never used the Addressables yet (currently I'm reading the documentation to get familiar with it), so I may be wrong. I just noticed that you're using
    Are you sure about this? Because I cannot find Instantiate overload where there is only one parameter.
    https://docs.unity3d.com/Packages/c...ityEngine.AddressableAssets.Addressables.html
    Although there is a LoadAsset method overload with only one parameter.

    ps.: if I'm wrong, feel free to ignore it, I don't want to hijack your thread
     
    Last edited by a moderator: Nov 25, 2018
  3. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    You can change your logic so instead of using instantiate you use LoadAsset, then on your Async_Completed you manually instantiate the asset. The reason it's being destroyed is because when instantiated with the addressables system it's saved into a dictionary on the system using the Sample1 scene. So when that scene is destroyed every addressable on that scene is released too. I'm not sure if they will change this logic anytime, but at least this way you can keep working on your project.

    Here's a quick test I did with your code and it seems to work fine (note that I used a primitive as a prefab so I'm not sure if it will work with any other more complex prefab)
    Code (CSharp):
    1. void Start()
    2.     {
    3.         DontDestroyOnLoad(gameObject);
    4.         var async = Addressables.LoadAsset<GameObject>("Prefab1");
    5.         async.Completed += Async_Completed;
    6.     }
    7.  
    8.     private void Async_Completed(UnityEngine.ResourceManagement.IAsyncOperation<GameObject> obj)
    9.     {
    10.         GameObject go = Instantiate(obj.Result);
    11.         DontDestroyOnLoad(go);
    12.         StartCoroutine(NextScene());
    13.     }
    You are right, but when used this way it uses the function:
    public static IAsyncOperation<TObject> Instantiate<TObject>(object key, Transform parent = null, bool instantiateInWorldSpace = false) where TObject : Object;
    Which can be used with the first parameter only.
     
    Lurking-Ninja likes this.
  4. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    That's right, and something we need to take into account. I've filed a bug in our system to see if we can avoid deleting things with that marking.
     
    Rotary-Heart likes this.
  5. Patrick_PS

    Patrick_PS

    Joined:
    Sep 9, 2019
    Posts:
    154
    [Unity 2018.4.18; Addressables 1.7.5]

    I am having a similar issues since I upgraded to 1.7.5. I have a few addressable scenes that contain GameObjects that are added to DontDestroyOnLoad during runtime. Once the scenes are unloaded these objects loose all references to sprites and prefabs. I did not have that issue prior to upgrading to 1.7.5.

    Is there a way to increase the ref count of those scenes so that they are never unloaded?

    edit: I upgraded from 1.6.2
     
    Last edited: Apr 3, 2020
  6. aka3eka

    aka3eka

    Joined:
    May 10, 2019
    Posts:
    32
    For those who has stumbled upon this problem, there is a solution in Addressables starting 1.9.2:
    https://docs.unity3d.com/Packages/c...al/AddressableAssetsAsyncOperationHandle.html
     
    drallcom3 likes this.
  7. drallcom3

    drallcom3

    Joined:
    Feb 12, 2017
    Posts:
    165