WebGL and Editor: Unloading 2 scenes one after another causing freeze.

Hi Guys,
I am having problem in unloading multiple scenes (2 scenes in my case) one after another. While{} loop in below code iterates only once, and do not work for the second time, where l_currentIndex=1, not even Debug.Log(…), first line inside while{}.

Please check if there any issue/bug in my code OR any Unity logic with SceneManager.UnloadSceneAsync(…). Thanks for your efforts.

private static IEnumerator IE_UnloadLevel(Scene[] a_sceneToUnload, Action a_callback)
{
            int l_sceneCount = a_sceneToUnload.Length;
            int l_currentIndex = 0;

            while (l_currentIndex < l_sceneCount)
            {
                 Debug.Log("Index: " + l_currentIndex);
                Scene l_unloadingScene = a_sceneToUnload[l_currentIndex];
                string l_unloadingSceneName = l_unloadingScene.name;

                //if current scene is the ACTIVE scene, then set other scene as ACTIVE and the Unload current.
                if (SceneManager.GetActiveScene().name.Equals(l_unloadingSceneName))
                {
                    Scene l_scene = GetPreviousLoadedScene(l_unloadingScene);
                    SceneManager.SetActiveScene(l_scene);
                }

                AsyncOperation l_operation = SceneManager.UnloadSceneAsync(l_unloadingScene);

                while (!l_operation.isDone)
                {
                    yield return null;
                }

                l_currentIndex++;
                yield return new WaitForSeconds(1.0f);    
            }

            Debug.Log("Unloading complete");

            if (a_callback != null)
            {
                Debug.Log("Calling callback");
                a_callback.Invoke();
            }
} //end of function

Are you sure that the object running that coroutine is not being unloaded too? Remember it doesn’t matter where the actual coroutine is located, but rather which MonoBehaviour instance is used to call .StartCoroutine(). That can be confusing.

SOLVED: Actually, Some script was using GameObjects(with AudioSource) of unloaded scene. So i had to destroy AudioSource first, then unload scene.

1 Like