Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Releasing multiple of the same addressable

Discussion in 'Addressables' started by gerasmussen, Jun 23, 2021.

  1. gerasmussen

    gerasmussen

    Joined:
    May 27, 2021
    Posts:
    32
    I am curious if there is a way to unload multiple instantiations of the same addressable using the same button.

    Code (CSharp):
    1. AssetReference m_reference;
    2.  
    3.     GameObject myGameObject;
    4.     AsyncOperationHandle<GameObject> _loadHandle;
    5.     AsyncOperationHandle<GameObject> _instatiateHandle;
    6.  
    7.     public string _keyToretrieve;
    8.  
    9.     public string KeyToRetrieve
    10.     {
    11.         get { return _keyToretrieve; }
    12.         set { _keyToretrieve = value; }
    13.  
    14.     }
    15.  
    16.     public void OnDownloadButttonPressed()
    17.     {
    18.             _loadHandle = Addressables.LoadAssetAsync<GameObject>(_keyToretrieve);
    19.             _loadHandle.Completed += OnLoadDone;
    20.     }
    21.  
    22.     public void Release()
    23.     {
    24.         Addressables.ReleaseInstance(myGameObject);
    25.     }
    26.  
    27.     public void OnDisable()
    28.     {
    29.         _loadHandle.Completed -= OnLoadDone;
    30.     }
    31.  
    32.     private void OnLoadDone(AsyncOperationHandle<GameObject> obj)
    33.     {
    34.         if (obj.Result != null && obj.Status == AsyncOperationStatus.Succeeded)
    35.         {
    36.             _instatiateHandle = Addressables.InstantiateAsync(_keyToretrieve);
    37.  
    38.             while (!_instatiateHandle.IsDone)
    39.             {
    40.                 Debug.LogWarning("INSTANTIATING");
    41.             }
    42.  
    43.             myGameObject = _instatiateHandle.Result;
    44.             myGameObject.name = _keyToretrieve;
    45.  
    46.         }
    47.  
    48.         if (obj.IsDone)
    49.         {
    50.             Addressables.Release(obj);
    51.         }
    52.  
    53.     }
    These are my on button click functions right now. With multiple clicks/calls of OnDownloadButtonPressed() I get multiple of the object. However, if I click/call Release() multiple times, it only releases the most recent instantiation of the object. How can I change this to continue releasing past instantiated objects?
     
  2. LilMako17

    LilMako17

    Joined:
    Apr 10, 2019
    Posts:
    43
    in your code sample above on line 43, you are setting myGameObject to the result value of the loaded object. By calling this function multiple times in a succession, you are overwriting whatever reference you had to your previous game object. Perhaps you could change myGameObject to a list?
     
  3. gerasmussen

    gerasmussen

    Joined:
    May 27, 2021
    Posts:
    32
    That makes sense to do in theory, but how would I keep track of the index that needs to be deleted? Should I maybe add to the list every time I instantiate? I'm fairly new to unity/c# in general.
     
  4. gerasmussen

    gerasmussen

    Joined:
    May 27, 2021
    Posts:
    32
    I am having trouble implementing my code with a list, as I am not finding methods to manipulate it the same way I am doing to my current game object.