Slowly destroying Tilemap

I’m trying to slowly destroy the tiles in a tilemap. Is there a way to wait 1 second after every iteration of this foreach loop?

code:

public List tileWorldLocations;
void Start(){
 
StartCoroutine(TestCo());
 
}
 
IEnumerator TestCo() {
 
Debug.Log("TestCo");
 
tileWorldLocations = new List<Vector3>();
 
foreach (var pos in tilemap.cellBounds.allPositionsWithin)
 
{
 
Vector3Int localPlace = new Vector3Int(pos.x, pos.y, pos.z);
 
Vector3 place = tilemap.CellToWorld(localPlace);
 
tilemap.SetTile(localPlace, null); // Removes all tiles
 
if (tilemap.HasTile(localPlace))
 
{
 
tileWorldLocations.Add(place);
 
}
 
yield return new WaitForSeconds(1);
 
}
 
 
 
print(tileWorldLocations);
 
}
 
}

Hello.

I did not read the code, but for your question, i understand do not know what COROUTINES are! You ned to learn them, its very very simple and easy. (100% recoomend spend next 15 minutes learning and trying to use it, no need more time)

A corutine is a function, that have a yield command on it, this means, the code stops when reach the yield command.

It can wait 1 frame, or some specific time.

For your example, you yould have a code like:

IEnumerator DestroyingTiles()
{    
    foreach(tile found you want to destroy)
    {
    Destroy();
    yield return new WaitForSeconds(1);
    }    
}

With this structure, when you call the corutine with

StartCoroutine(DestroyingTiles());

The code will find the first tile, destroy it, and wait 1 second, before continue with the second tile, then wait again, continue with the 3rd, etc…

Go investigate, in less than 1 hour you will be a corutines expert!! :smiley:

BYEE!