Coroutine/Nested For Loop Yield

I have a coroutine with some nested for loops, the idea being to work through each point in a 3 dimensional array and do some stuff, but to yield the coroutine after each point so that it doesn’t halt the game while it works through the array (which is pretty big). Here’s some code;

public IEnumerator PopulateArray () {

    for (int x = 0; x < maxX; x++) {
       for (int y = 0; y < maxY; y++) {
          for (int z = 0; z < maxZ; z++) {
    
             // Do some stuff
    
             // Yield until the next frame
    
          }
       }
    }
}

EDIT: Added following information based on comments.

When I try “yield return null”, I get the following errors;

  • “error CS0037: Cannot convert null to ‘bool’ because it is a value type”
  • “error CS1662: Cannot convert ‘iterator’ to delegate type ‘code.PopulateArray()’ because some of the return types in the block are not implicitly convertible to the delegate return type”

And when I try "yield on its own I get;

  • “error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement”

Thank you in advanced.

Since your code is written in C# you have to use

yield return null;

to wait for one frame in a coroutine. If you have written it like this, the line is certainly not the problem. The only thing that could cause an “error” on that line is when you use the yield statement in a normal method. You can only use it inside a “generator” method which must have the return type “IEnumerator”.

IEnumerator MyCoroutine()
{
    for (int x = 0; x < maxX; x++) {
        for (int y = 0; y < maxY; y++) {
            for (int z = 0; z < maxZ; z++) {
                // do stuff
                yield return null;
            }
        }
    }
}

It looks like this question was a lame duck. After trying all teh things (swapping for for while, trying WaitForSeconds(0) instead of null, etc), I refreshed the assets in the Unity project and now it seems to be working. Sorry for wasting you guys time, but thanks for trying to help.