Destroy GameObject using Tag and Specific Location

I have a tetris-like matching game. When three rows are matched, I want them to be destroyed. Each colored block has its own unique tag “GreenBlock” , “RedBlock” etc. I already have successfully made the game identified whenever three same-colored blocks are within each other.

To visualize the screen, I made a custom Grid System using GameObjects with BoxCollider 2d:

| C1 | C2 | C3 | C4 | C5 |

| B1 | B2 | B3 | B4 | B5 |

| A1 | A2 | A3 | A4 | A5 |

Now, for example on A1-A2-A3, three green blocks were matched. Those green blocks have the same “GreenBlock” tag, alongside with all the green blocks on the screen. I only wanted to destroy the “GreenBlocks” tagged gameObjects on positions A1-A2-A3. I cannot just say delete all “GreenBlocks” tagged objects since it would delete all Green Blocks on the screen even if they are not matched. I cannot also do the “inspector method” (dragging items their manually on public variables before running the game) since all the blocks are generated randomly.

How can I delete those same-tagged objects on a specific location?

Hi @DranrebKing - @NoDumbQuestion already provided some good links. You should familiarize yourself with the concepts first - learn about:

  • Arrays and Lists
  • Multidimensional arrays
  • Iteration of arrays
  • How to read array variables
  • how to spawn and remove gameobjects (or recycle them from object pool)

Note that this is no way fully working code, you should definitely first read the articles provided in comments and learn how things work.


The idea is that you create the playfield from data, then do the checking of valid moves, neighbours, removal of tiles and such using something like the CheckTileAt method here. After each frame’s updates, whatever the game logic does, you’ve done changes to your game playfield data, and you have to update “the visuals”. Here I just once instantiate gameobjects in Start, to match the playfield data. in reality, you’d simply update/remove/respawn needed block Gameobjects each frame.


// create playfield data 3x4
// tile type defined in 1,2,3 int values
public int[,] tiles =  
{ 
	{1, 3, 1 }, 
	{1, 3, 1 }, 
	{1, 2, 2 }, 
	{1, 1, 2 } 
};

// Prefab used to display the tile data
public GameObject blockPrefab; 

void Start () 
{
	// Update blocks from data
	UpdateBlocks();

	// How to read tiles in x,y position of array
	Debug.Log("Tile at 0,0 is:" + CheckTileAt(0,0));
	Debug.Log("Tile at 1,2 is:" + CheckTileAt(1,2));
	Debug.Log("Tile at 2,3 is:" + CheckTileAt(2,3));
}


void UpdateBlocks ()
{
	var x = 0;
	var y = 0;

	// Iterate all tiles of x,y array - for loop might be better
	foreach (var t in tiles)
	{
		// Instantiate block
		var block = Instantiate(blockPrefab) as GameObject;

		// Set material by tile type
		if (t == 1)
			block.GetComponent<Renderer>().material.color = Color.red;
		else if (t == 2)
			block.GetComponent<Renderer>().material.color = Color.blue;
		else if (t == 3)
			block.GetComponent<Renderer>().material.color = Color.yellow;

		// Set block position
		block.transform.position = new Vector2(x,-y);
					
		x++;
		
		if (x == 3) 
		{
			x = 0;
			y++;
		}
	}
}

// Helper
int CheckTileAt (int x, int y)
{
	return tiles[y,x];
}