Destroying 2D Array of Instantated Objects

I’m having trouble with destroying a 2D array of instantiated Prefabs. I searched around here and there were many questions that were very similar but their answers simply do not work for me.

I’m making a grid of plane objects at run time. What i want to be able to do is generate a grid of a certain size, then be able to regenerate the grid at another size. the problem is, despite looping through the grid and calling Destroy on each one, it just keeps making a new grid on top of the grid already there.

Generation Code:
private void GenerateGrid()
{

		for (int zz = 0; zz < gridWidthandHeight; zz++)
		{
			for (int xx = 0; xx < gridWidthandHeight; xx++)
			{
				grid[xx, zz] = (GameObject)Instantiate(_squarePrefab);

				grid[xx, zz].transform.position = topLeftPosition +
					new Vector3(squareDimensions.x * xx, 0, squareDimensions.z * zz);
			}
		}
	}

Destruction Code:

private void DestroyGrid()
	{
		for (int zz = 0; zz < gridWidthandHeight; zz++)
		{
			for (int xx = 0; xx < gridWidthandHeight; xx++)
			{
				if (grid[xx, zz] != null)
				{
					Destroy(grid[xx, zz]);
				}
			}
		}
	}

I call the Destroy function before the generate function every time i call to make the grid:

public void BuildGridObject(int widthAndHeight)
	{
		if (widthAndHeight % 2 != 1)
		{
			widthAndHeight += 1;
		}

		gridWidthandHeight = widthAndHeight;

		grid = new GameObject[gridWidthandHeight, gridWidthandHeight];

		position = this.gameObject.transform.position;
		squareDimensions = _squarePrefab.renderer.bounds.size;

		topLeftPosition = position -
			new Vector3(
			(gridWidthandHeight * squareDimensions.x) / 2,
			0,
			(gridWidthandHeight * squareDimensions.z) / 2) +
			new Vector3(
				squareDimensions.x / 2,
				0,
				squareDimensions.z / 2);

		DestroyGrid();
		GenerateGrid();

		bounds = new Bounds(position, new Vector3(squareDimensions.x * gridWidthandHeight, 1, squareDimensions.z * gridWidthandHeight));
	}

I have A set to make a 21x21 grid, S to make a 31x31 grid, D to make a 41x41 grid and F to make a 51x51 grid.

Every time I press one of those keys it should get rid of the grid that’s there and make the one, but its obviously not doing that.

I’m building this game as a way to get myself familiarized with Unity, so there must be something i’m missing, but i can’t figure out what. The other answers to similar questions were things like destroying the rigid body and not the game object, but my grid is a grid of game objects, by everything I’ve found this should work.

On line 10 you’re creating a new array of GameObjects as your new grid. When you create a new array for reference types, each reference is defaulted to null. So when you create the new grid, you’re also forgetting the old grid.

Then later on line 25 you try to destroy the old grid, which you have forgotten by creating a new grid.

The solution is simply: Destroy the grid before you create a new one.

Note the difference between “create” and “generate”

I see two problems here. On line 10, you initialize ‘grid’. If grid contained a reference to an existing grid, you just blew that reference away. You would want to do a destroy before you created the new grid. The second issue is a bit more subtle. Since you are passing in ‘widthAndHeight’, and setting that for the class, if your width and height changes, then you would either not destroy some game objects, or you could get an out of bounds error. You can fix this by moving DestoryGrid() call to line 7 (before you set ‘gridWidthAndHeight’) so that you are using the values before the array is resized. In fact, it would be better if the DestoryGrid() took its sized directly from the array rather than depend on this variable.

And if you move DestoryGrid() back to line 7, you will need to make it contingent on ‘grid’ not being null.