Hey guys!
I’m getting a really weird error when unloading scenes and I don’t know if I’m doing anything wrong. I’m creating a package in Unity and I have some scenes inside this package. When I try to load these scenes, everything works fine, but when I try to unload them, I get an error that says:
ArgumentException: Scene to unload is invalid
My code looks like this:
public virtual async Task UnloadLocalScene(string scene)
{
if (!IsSceneLoaded(scene))
return;
AsyncOperation operation = UnityEngine.SceneManagement.SceneManager.UnloadSceneAsync(scene);
while (!operation.isDone) await Task.Yield();
Debug.Console.Log($"[SceneManager] Scene {scene} unloaded");
}
protected virtual bool IsSceneLoaded(string scenePath)
{
string sceneName = Path.GetFileNameWithoutExtension(scenePath);
Scene scene = UnityEngine.SceneManagement.SceneManager.GetSceneByName(sceneName);
return scene.IsValid() && scene.isLoaded;
}
It is important to notice the code always works with scenes outside packages (in Assets). I understand that this may be something you are not intended to do since packages have Samples, but I’m curious about why this may be happening.
Thank you!
PD: This error is happening to me in unity2022 and unity 6