Loading screen coroutine stops executing for apparently no reason

I’m making my first 2D topdown shooter game. I’m working on the loading part of the game. I have this functioning loading screen function (a method of my GameManager class) I made. I tried swapping between the main title scene and the first level a few times to test it. After loading the first level, then the title screen, when I try loading the first level back again the load screen is stuck.
The whole thing is working on a coroutine, and after some debugging I figured out the problem was that this coroutine stopped executing in a certain part of the code for some reason.

    public void LoadScene(int sceneIndex)
    {
        // If another scene is already loading, abort
        if (isLoadingScene)
        {
            Debug.LogWarning($"Attempting to load scene {sceneIndex} while another scene is already loading, aborting");
            return;
        }

        isLoadingScene = true;

        var sceneLoader = FindObjectOfType<SceneLoader>().gameObject;

        if (sceneLoader != null)
        {
            // Make the loading screen appear
            var animator = sceneLoader.GetComponent<Animator>();
            animator.SetTrigger("appear");

            // Make sure the SceneLoader object is maintained until the next scene
            // in order to not make the animation stop
            DontDestroyOnLoad(sceneLoader);
        }
        else // If a SceneLoader is not found in the current scene, throw an error
        {
            Debug.LogError($"SceneLoader could not be found in {SceneManager.GetActiveScene().name}");
        }

        // Unload active scene and load new one
        StartCoroutine(LoadScene_(sceneIndex, sceneLoader));
    }
   
    IEnumerator LoadScene_(int sceneIndex, GameObject sceneLoader)
    {
        float t = Time.time;

        // Start loading the new scene, but don't activate it yet
        AsyncOperation load = SceneManager.LoadSceneAsync(sceneIndex, LoadSceneMode.Additive);
        load.allowSceneActivation = false;

        // Wait until the loading is finished, but also give the loading screen enough
        // time to appear
        while (load.progress < 0.9f || Time.time <= (t + LoadingScreen_appearTime))
        {
            yield return null;
        }

        // Activate loaded screen
        load.allowSceneActivation = true;

        // Unload old scene
        AsyncOperation unload = SceneManager.UnloadSceneAsync(SceneManager.GetActiveScene());

        // Wait until it's finished unloading
        while (!unload.isDone) yield return null;
        // --- THE COROUTINE STOPS HERE DURING THE 3RD LOADING --- //

        // Make the loading screen disappear
        sceneLoader.GetComponent<Animator>().SetTrigger("disappear");

        // Wait until the disappearing animation is over, then destroy the SceneLoader object
        // which I set to DontDestroyOnLoad before
        yield return new WaitForSeconds(LoadingScreen_disappearTime);

        Destroy(sceneLoader);
        isLoadingScene = false;
    }

Only during the 3rd loading, right after the while (!unload.isDone) yield return null; line the coroutine just stops executing (I know it because I inserted a Debug.Log inside the while loop and it got called, but I inserted one right after and it did NOT get called).

Any suggestions?

So, from your description and code, my best guess is that where you set unload.isDone is where your problem is. I don’t think it’s possible to tell where you went wrong without looking at that part.

Shouldn’t you cache the “old scene” at the start of the coroutine instead of using GetActiveScene() after the other scene has finished loading? I think there’s a race condition where you’re trying to load and unload the same scene.

Tried adding Scene oldScene = SceneManager.GetActiveScene() after line 35 and then changing line 62 to AsyncOperation unload = SceneManager.UnloadSceneAsync(oldScene), didn’t change a thing. Issue still remains.