...
Debug.Log($"Attempting to unload old scene ({sceneInstance_current.Scene.name}) and load new scene...");
Addressables.UnloadSceneAsync(sceneInstance_current).Completed +=
(AsyncOperationHandle<SceneInstance> sceneUnloaded_callback) =>
OnSceneUnloaded(sceneUnloaded_callback);
Throws:
Addressables.UnloadSceneAsync() - Cannot find handle for scene UnityEngine.ResourceManagement.ResourceProviders.SceneInstance
UnityEngine.AddressableAssets.Addressables:UnloadSceneAsync (UnityEngine.ResourceManagement.ResourceProviders.SceneInstance,bool)
Preceding Debug.Log entry verifies that “sceneInstance_current” is indeed present. The behavior persists regardless of scene cached in this variable.
The SceneInstance to handle is added to the cache of scene loads upon Scene load complete. It will also be removed from the handles cache if the Scene was unloaded by other means, such as if you previously did a LoadSceneAsync( “otherscene”, SceneLoadMode.Single). Because SceneLoadMode.single will unload other scenes. If you load another scene before this, the scene you are trying to unload will already be unloaded and removed.
In your code here:
Debug.Log($“Attempting to unload old scene ({sceneInstance_current.Scene.name}) and load new scene…”);
The main part to know is if the scene is actually loaded. Try using sceneInstance_current.Scene.isLoaded instead. You have have a reference to a Scene without it being loaded.
Hi Andy. Thanks for getting back to me on this.
Following your guidance, I’ve found that the sceneInstance_current.Scene.isLoaded == true.
This is the state it should be in for it to be unloadable, correct?
Yes, although it is possible you are trying to unload it before Addressables has received it to be tracked. Or using WaitForCompletion can have issues (Scenes must be async). I will take the assumption that you are not doing these for now.
So long as you have released the scene in any way or unloaded it with the SceneManager, then I do not know of any issues.
The quickest way to resolve it would be to avoid actually doing the handle lookup from the SceneInstance, and using the AsyncOperationHandle created from the LoadSceneAsync call instead.
It does look like a bug however, If you can create a bug report for the issue then we can investigate it locally.
I’ve tried the alternative approach of using the cached instance of AsyncOperationHandle, instead of SceneInstance, but it throws the following exception:
“Exception: Attempting to use an invalid operation handle”
This indicates that the handle has already be released. This can happen by the Scene becoming unloaded.
The most common way of this happening where you do not explicitly call unload yourself, is by loading another Scene. If you load a Scene using LoadSceneMode.single then all other Scenes will be unloaded and the handle will be released as this is detected.
Yeah the more I play with this, the more convinced I am that it’s a bug.
When playing in the editor, these scenes are confirmed as being loaded and functional.
The following code confirms that the scene is loaded. It loads the new scene properly, and attempts to unload the old scene, but fails for some unknown reason.
private void ExecuteSceneSwap()
{
if (sceneInstance.Scene.isLoaded) Addressables.UnloadSceneAsync(sceneInstance).Completed += (AsyncOperationHandle<SceneInstance> sceneUnloaded_callback) => OnSceneUnloaded(sceneUnloaded_callback);
else Debug.LogError("Scene to be unloaded is already unloaded.");
Addressables.LoadSceneAsync(sceneAssetCache, LoadSceneMode.Additive).Completed += (AsyncOperationHandle<SceneInstance> sceneLoaded_callback) => OnSceneLoaded(sceneLoaded_callback);
}
private void OnSceneUnloaded(AsyncOperationHandle<SceneInstance> sceneUnloaded)
{
if (sceneUnloaded.Status == AsyncOperationStatus.Failed) Debug.LogError("Addressables async scene unload failed.");
}
private void OnSceneLoaded(AsyncOperationHandle<SceneInstance> sceneLoaded)
{
if (sceneLoaded.Status == AsyncOperationStatus.Succeeded)
{
sceneInstance = sceneLoaded.Result;
}
}
I filed a bug report yesterday, so hopefully the team picks it up.