Search Unity

Unload scene exception

Discussion in 'Addressables' started by video21region, Feb 24, 2020.

  1. video21region

    video21region

    Joined:
    Sep 5, 2019
    Posts:
    10
    Hi. Why I unload scene and get exception? Attempting to use an invalid operation handle
     

    Attached Files:

  2. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    You are assigning the asyncOperation when the operation completes, not when it's started. I'm guessing you're trying to unload it while it's still loading, so the variable is invalid during that time.

    As an aside, it helps if you can paste code on the forums instead of a screenshot of code.

    [EDIT] Looking more at your code, I see no reason why it shouldn't work, assuming the handle that's passed into Completed is a valid handle. Try assigning the handle when you start the operation instead of when complete and see if that helps.

    I think you should file a bug report for this issue if you're using the latest version.
     
    Last edited: Feb 24, 2020
  3. video21region

    video21region

    Joined:
    Sep 5, 2019
    Posts:
    10
    [FIXED] I have to save the result, not the async operation.


    Code (CSharp):
    1.     public class MenuAdditiveSceneLoader : MonoBehaviour
    2.     {
    3.         [Title("Settings")]
    4.         [SerializeField] private MenuAdditiveSceneLoaderSettings settings;
    5.        
    6.         [Title("References")]
    7.         [SerializeField] private OnlineGameMode onlineGameMode;
    8.  
    9.         private SceneInstance registrationSceneInstance; //previous: private AsyncOperationHandle<SceneInstance> registrationSceneInstance;
    10.         private Registration registration;
    11.  
    12.         public event Action OnRegistrationSceneLoaded;
    13.         public event Action OnRegistrationSceneUnloaded;
    14.  
    15.         private void OnEnable()
    16.         {
    17.             onlineGameMode.OnAccountNotFound += LoadRegistrationScene;
    18.         }
    19.  
    20.         private void OnDisable()
    21.         {
    22.             onlineGameMode.OnAccountNotFound -= LoadRegistrationScene;
    23.         }
    24.  
    25.         private void LoadRegistrationScene()
    26.         {
    27.             Addressables.LoadSceneAsync(settings.RegistrationSceneName, LoadSceneMode.Additive).Completed
    28.                 += OnRegistrationSceneLoadingDone;
    29.         }
    30.  
    31.         private void UnLoadRegistrationScene()
    32.         {
    33.             registration.OnRegistrationEnded -= UnLoadRegistrationScene;
    34.  
    35.             Addressables.UnloadSceneAsync(registrationSceneInstance).Completed += OnRgistrationSceneUnloadingDone;
    36.         }
    37.  
    38.         private void OnRegistrationSceneLoadingDone(AsyncOperationHandle<SceneInstance> asyncOperation)
    39.         {        
    40.             registration = FindObjectOfType<Registration>();
    41.             registration.OnRegistrationEnded += UnLoadRegistrationScene;
    42.  
    43.             registrationSceneInstance = asyncOperation.Result;  //previous: registrationSceneInstance = asyncOperation;
    44.  
    45.             OnRegistrationSceneLoaded?.Invoke();
    46.         }
    47.  
    48.         private void OnRgistrationSceneUnloadingDone(AsyncOperationHandle<SceneInstance> asyncOperation)
    49.         {
    50.             OnRegistrationSceneUnloaded?.Invoke();
    51.         }
    52.     }