I have created a procedurally generated 'tiled floor' in unity3d, using a block prefab asset and script as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Wall : MonoBehaviour
{
public GameObject block;
public int width = 10;
public int height = 10;
public int timeDestroy = 1;
List<GameObject> blockList = new List<GameObject>();
void Start(){
for (int y=0; y<height; ++y)
{
for (int x=0; x<width; ++x)
{
Vector3 offset = new Vector3(x, 0, y);
GameObject hello= (GameObject)Instantiate(block, transform.position + offset, Quaternion.identity);
blockList.Add(hello);
}
}
StartCoroutine(SelfDestruct());
}
void Update()
{
SelfDestruct();
}
IEnumerator SelfDestruct()
{
yield return new WaitForSeconds(timeDestroy);
Destroy(blockList[UnityEngine.Random.Range(0,(width*height))]);
}
}
When I run the game, one of the 100 blocks has been destroyed, but then nothing happens. From my script, I was expecting one block to destroy every second, as defined by:
yield return new WaitForSeconds(timeDestroy);
where int timeDestroy = 1;
and repeat until all the blocks are gone - game over. How can I change my script so the 100 gameObjects are destroyed one after another, until none are left?