how to pause and continue for loop without breaking it

Hi, I am beginner in coding and I was writing a code where I need to pause and continue for loop when conditions are met. The idea is I have a code that spawning tiles from certain zones that chooses randomly, and there is limit number of tiles that can be spawned, so I want to make for loop that spawning tile and when hit limit wait and after tiles will be destroyed continue spawning tiles that until it spawn all tiles according to zone length. However for some reason when for loop hit limit, the loop is ended. I try to make pause by using WaitUntil function with condition. So any advises on how to properly make that are welcome.
Here is my function that spawn tiles:

 void Zone(GameObject[] zone)
    {
        ZoneLenght = Random.Range(5,20);
        for (int i = 0; i < ZoneLenght; i++)
        {
            if (TileNumber == TileLimit)
            {
                StartCoroutine(WaitFreeSpace());
               
            }
            else
            {
                Debug.Log($"i={i}");
                SpawnTile(zone);
                TileSpawned++;
               
            }
           
        }

        Debug.Log("Leaved loop");
        if (TileSpawned == ZoneLenght)
        {
            ZoneSpawned = true;
            TileSpawned = 0;
        }

       

    }

I put some debug options to check how many times for loop was runnig and also when it leaves loop. After loop I put if statement to check if all tiles from certain zone was spawned. And it successfully spawn all tiles correctly before limit of 20 tiles after that it just abandon loop.
Here is my function that I use to pause loop:

 IEnumerator WaitFreeSpace()
    {
        Debug.Log("Waiting");
        yield return new WaitUntil(() => TileNumber < TileLimit);


    }

So After it hits limit it is waiting for some time, but after that it just leaves the loop
I also add screenshot with log. On the log it shown that loop stops on nine and after that waits, However after it waits for three times it leaves the loop. Also on the right side it is showed that it has spawned 10 tiles from 14 and even though free space appeared it is not spawning

coroutines cannot pause the code that called StartCoroutine. They can only pause themselves. Your entire loop needs to be INSIDE a coroutine. Then you can use the yield statement to pause.

Thank you for advice. It worked, however when I paste my code in coroutine it was not still working correctly because of my if statement, so I also needed to fix that. I remove else because of that I either wait or spawn and because of that my loop did not pause. But after I fixed that it started working correctly.

Note that IEnumerators can also be used without Unity’s coroutine concept, just as a mechanism to “pause” and resume a more complex piece of code. So you can simply throw a “yield return” in your code whenever you want to “suspend” your code. However that of course means you have to store your IEnumerator instance yourself in a variable and simply call MoveNext on it in order to “advance” your code to the next yield.

IEnumerator MyCode()
{
    for(int i = 0; i < 1000; i++)
    {
        // Do stuff
        if (i %100 == 99)
            yield return null;
    }
}

IEnumerator obj = MyCode();


// advance your code to the next yield
obj.MoveNext();

Here you have to call MoveNext 11 times to complete your code. Once completed it can not be “resumed”. Note that none of your code will be called when you create your IEnumerator instance with this line: IEnumerator obj = MyCode();. When you call your first MoveNext it will actually run your code up to the first yield statement. That’s all there is to IEnumerators. For more information see my coroutine crash course.

2 Likes