Nested Coroutine doesn't appear to stop when it is suppose to.

I currently have a coroutine that generates my world, which is almost working great! However I have another coroutine that I use to delete everything in the world if there ends up being a problem with the build process.

Within an if statement, if a condition is met the following code is run:

yield return StartCoroutine(ResetDungeonGeneration());
Debug.Log("We finished deleting");	

The coroutine code looks like this:

	IEnumerator ResetDungeonGeneration() {		
		Debug.Log("<color=red>**** STARTING DUNGEON RESET ****</color>");
		var dungeon = GameObject.Find("Dungeon");
		var dungeonPrefabs = dungeon.transform.childCount;	
		for(int i = 0; i < dungeon.transform.childCount; i++) {
			Debug.Log("There are " + dungeon.transform.childCount + " to delete.");
			Debug.Log("<color=red>*** Deleting " + dungeon.transform.GetChild(i).name + " ***</color>");
			Destroy(dungeon.transform.GetChild(i).gameObject);						
		}		
		yield return null;
	}

The deleting portion works fine. I see the expected logs in my console, the objects are removed from my inspector window. However then Unity just stops there. It doesn’t crash, I can still interact with things but I never see the message Debug.Log(“We finished deleting”);

If I add another log statement inside the ResetDungeonCoroutine right before the yield return null, it logs properly which makes it seem like it never considers itself finished. I am not noticing any errors so it must be something I am misunderstanding. Any help would be appreciated.

First of all in it’s current form it’s pointless for “ResetDungeonGeneration” to be a coroutine. The code before the yield is executed immediately and you don’t do anything after the yield. So it can be a normal method unless you plan to spread certain actions across multiple frames. If not, make it a normal method and just call it the usual way.

If a coroutine stops suddenly at a yield statement, that means one of the following things has happend:

  • You have deleted the gameobject where the coroutine was running on.
  • You called StopCoroutine / StopAllCoroutines on the gameobject the coroutine was running on.
  • You deactivated the gameobject the coroutine was running on.
  • You deleted the MonoBehaviour that was used to start the coroutine.

Note: “StartCoroutine” is a member function of the MonoBehaviour class. When you use it to start a coroutine, that coroutine runs on that MonoBehaviour instance that was used to start it. It doesn’t matter in which class the coroutine is located, only from where it has been started