Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Addressables and DontDestroyOnLoad doesn't work

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

  1. gw1108


    Jan 22, 2013
    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;
    5. public class Global : MonoBehaviour {
    7.     // Use this for initialization
    8.     void Start () {
    9.         DontDestroyOnLoad(gameObject);
    10.         var async = Addressables.Instantiate<GameObject>("Prefab1");
    11.         async.Completed += Async_Completed;
    12.     }
    14.     private void Async_Completed(UnityEngine.ResourceManagement.IAsyncOperation<GameObject> obj)
    15.     {
    16.         DontDestroyOnLoad(obj.Result);
    17.         StartCoroutine(NextScene());
    18.     }
    20.     private IEnumerator NextScene()
    21.     {
    22.         yield return new WaitForSeconds(1f);
    23.         UnityEngine.SceneManagement.SceneManager.LoadScene("Sample2");
    24.     }
    25. }
  2. Lurking-Ninja


    Jan 20, 2015
    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.
    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: Nov 25, 2018
  3. Rotary-Heart


    Dec 18, 2012
    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.     }
    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 Technologies

    Apr 11, 2017
    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.