Coroutine stops after while-loop

I have a singleton LevelManager loading a level, waiting for a script from the newly-loaded level to assign a gameObject to the LevelManager to then do stuff with it.

I have the following code:

//some gameObject calls the loadLevel coroutine
void somefunction(sceneToLoad){
    StartCoroutine(LevelManager.Instance.loadLevel (sceneToLoad));
}

//snippet of LevelManager.cs
public GameObject levelPrepper = null;
public IEnumerator loadLevel(string levelName){
    Application.LoadLevel (levelName);
    while (!levelPrepper)
        yield return null;
    yield return StartCoroutine (waitForLevelPrepper());
    print("yay");
    //do stuff
}

//snippet of the levelPrep.cs:
void Awake(){
    LevelManager.Instance.levelPrepper = gameobject;
}

The problem is, “yay” never gets printed. I’ve done some reading and found that this might happen when the gameObject carrying the coroutine is destroyed. However, LevelManager is definitely never destroyed during the process, so I’m at a loss.

Does the new level being loaded have a LevelManager that’s being destroyed by the singleton? If so are you certain that it’s not changing the Instance variable before it does? Remember that Destroy() actually happens at the end of frame, so any code under it is still run.

Also are you sure it’s not the waitForLevelPrepper() coroutine that’s holding things up? The loadLevel coroutine will wait until it’s finished before running the print() line. You can add a print() above that StartCoroutine() to check.

Finally in levelPrep, I’d add a print() to make sure it’s actually being run. You’re also trying to assign ‘gameobject’, shouldn’t that be ‘gameObject’? If it’s a variable you made, double check that it’s assigned itself.