I decided to make this 5x5 grid:
public GameObject gridBlock;
public static int width = 5;
public static int height = 5;
private GameObject[,] grid = new GameObject[5, 5];
public GameObject theHandler;
//FUNCTION --- SPAWN
public void SpawnGrid()
{
//Spawn Grid
for (int x=0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
//X Block Grid
Vector3 startPos = theHandler.transform.position;
GameObject gridTile = (GameObject)Instantiate(gridBlock);
gridTile.transform.position = new Vector3(startPos.x + x, startPos.y - y, 0);
}
} // End of Loop
}
Now I wanted to a delete a specific grid block (gameObject) that has the position of (1, -2) on the map.
I tried using this:
//UPDATE
void Update()
{
if(Input.GetKeyDown("space"))
{
DestroyBlock( 1, -2);
}
}
//FUNCTION --- DESTROY
public void DestroyBlock(int x, int y)
{
GameObject.Destroy(grid[x,y].gameObject);
}
It gives me a “out of range array” error. Any thoughts on this?