yield return null completely breaking out of coroutine

I am currently running into an issue where it seems a “yield return null” is completely breaking out of my coroutine rather than continuing where it left off.

I am starting my coroutine within the Start() with a piece of code that looks like this:

buildRoutine = GenerateDungeon();

This part works well. Within my GenerateDungeon code I have a collision check when an object is placed to make sure that it is not overlapping with another object. I am currently telling it to try three times before I assume that I’ve created an impossible situation.

My code destroys the overlapping object properly and then I use a yield return null to wait a frame before I try to instantiate a new one. This is because I learned that a deleted object in code still exists until a new frame starts. The yield was meant to wait that one frame. That piece of code is the following:

destroyedSomething = "We destroyed something";
yield return null;

destroyedSomething is just an empty string declared at the top of the file just for debugging purposes. The code is suppose to attempt to place a new object 2 more times before the build is considered failed. However it immediately jumps to the code that exists right below my StartCoroutine function which is this:

if(dungeonSuccess == false) {
	Debug.Log("Starting new build - " + destroyedSomething);

This means it is completely breaking out of the GenerateDungeon coroutine long before it is suppose to. I have other log statements through out the build process that are not appearing so I know it’s not reaching anywhere else in the build.

My understanding was that yield return null was suppose to wait one frame before continuing where it left off with all the information intact but it is completely exiting my coroutine and I can’t seem to figure out why.

You will need to either use a while loop to wait and iterate, or WaitUntil() with a lambda expression to wait until the conditions in the expression are met.

Yield return null just sets the coroutine’s state to null, and it’s definitely no break or waiting loop at all.