Search Unity

Question How to release multiple addressables from the same or multiple label?

Discussion in 'Addressables' started by GlaDos_sae, May 8, 2023.

  1. GlaDos_sae

    GlaDos_sae

    Joined:
    Nov 11, 2021
    Posts:
    1
    Sorry if this question was asked before. Couldn't find it in the forum.

    Problem: I Instantiate some GameObjects(Addressables) inside a grid. My plan is now to release all spawned GameObjects with a button for example. Somehow I don't get my head around how to do this the proper way. Currently, I get this error when I use Addressables.Release(_loadHandle): Attempting to use an invalid operation handle.

    Has someone an Idea what I'm doing wrong?

    Code (CSharp):
    1. private void DoVoxelGrid() {
    2.         for (int x = 0; x < width; x++) {
    3.             for (int y = 0; y < height; y++) {
    4.                 for (int z = 0; z < length; z++) {
    5.                     var pos =  center + new Vector3( x, y, z ) * threshold;
    6.  
    7.                     _loadHandle = Addressables.LoadAssetsAsync<GameObject>(assetLabelReferences, addressable => {
    8.                         Instantiate(addressable, pos, Quaternion.identity);
    9.                     }, Addressables.MergeMode.Union, true);
    10.                 }
    11.             }
    12.         }
    13.     }
     
  2. andymilsom

    andymilsom

    Unity Technologies

    Joined:
    Mar 2, 2016
    Posts:
    294
    The problem here is that you will be replacing the _loadHandle with every loop. Each iteration will call load again, incrementing the reference count and replacing the handle with a new one. If doing a similar method to release, the first release would release the last handle returned and invalidate the handle.

    You should do the looping inside the completion callback in this case.