Coroutine won't continue after nested coroutine finishes.

I think I need help with Coroutines:

When the player dies, I call a Coroutine to inform the overall Game Administration object that the player is dead:

StartCoroutine(BattleInstance.instance.PlayerKilled());

In there I do a bunch of administrative things like stopping the spawning of enemies and more (which work fine).

    public IEnumerator PlayerKilled()
        {
            Cursor.visible = true;
            WaveManager.instance.PauseBattle();
            if (_costToRevive != 0)
            {
                yield return StartCoroutine(BattleViewController.instance.ShowContinuePopup(_countdownToRevive, _costToRevive));
            }
            yield return StartCoroutine(EndGame());
        }

My problem is with the coroutine I call to show my “Continue?” pop up:

yield return StartCoroutine(BattleViewController.instance.ShowContinuePopup(_countdownToRevive, _costToRevive));

Theoretically after that one is finished (basically once the continue timer has run to 0, there’s supposed to be the coroutine started which handles my scene switching, player saving and some other administrative things:

yield return StartCoroutine(EndGame());

Unfortunately my code never gets there. From what I was able to see so far when my ShowContinuePopup coroutine ends, it just ends… and the PlayerKilled coroutine doesn’t continue.

Here’s the last coroutine that finishes:

public IEnumerator ShowContinuePopup(int timer, int? cost = null)
    {
        yield return StartCoroutine(_continuePopup.Countdown(timer, cost));
        _continuePopup.HideContinuePopUp();
    }

I have tried a bunch of things including adding a ‘yield return null;’ in the end of that last method or making the HidePopUp method a coroutine and yield for that… nothing helps. When I set my _costToRevive to 0 to bypass the ShowContinuePopUp coroutine, it works, so it must be somewhere after hiding the popup and calling the EndGame coroutine… But there’s nothing there. :frowning:

Any ideas why?

The start of my coroutines

StartCoroutine(BattleInstance.instance.PlayerKilled());

Was in my player object. Since the player object got Destroyed() after starting the coroutines the PlayerKilled() routine apparently was killed as well. So when I tried to return to it there was nothing left to return to.

Fix:

public IEnumerator PlayerKilled()

was turned into a void and called regularly from the player object. Inside that method I simply called one coroutine:

public void PlayerKilled()
    {
        StartCoroutine(ShowContinueAndEndGame());
    }

Which then contained my actual coroutines to call:

public IEnumerator ShowContinueAndEndGame()
    {
        Cursor.visible = true;
        WaveManager.instance.PauseBattle();
        if (_costToRevive != 0)
        {
            yield return StartCoroutine(BattleViewController.instance.ShowContinuePopup(_countdownToRevive, _costToRevive));
            Debug.Log("After Continue yield");
        }
        Debug.Log("starting to yield for EndGame");
        yield return StartCoroutine(EndGame());
    }

That way the coroutines belonged to the BattleInstance singleton and were able to complete regularly!