- Home /
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);
}
}
Answer by tormentoarmagedoom · Apr 04, 2020 at 12:05 PM
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!! :D
BYEE!
I'm already using a coroutine it's just that for some reason the coroutine is not going into the foreach loop
Your answer
Follow this Question
Related Questions
Prefab to a tile script 0 Answers
Huge lag when toggling between Tile maps in one scene. 0 Answers
How can I modify the sprite of a particular Scriptable Tile in a Tilemap? 2 Answers
Creating a 2d RPG terrain tilemap from earth map/imagery? 1 Answer
Addressable.LoadAssetAsync doesn't work but has no error 0 Answers