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