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?
How can I target a gameObject on an exact Vector2 Position? Like if(gameObjectExistsOn(0.5f, 1.5f)
?
you don't.
You tell it to destroy block like B5, A3.
That's why i send you link to learn about array and simple 2D matrix.
@Dranreb$$anonymous$$ing - what @NoDumbQuestion is saying - don't try to use visuals (gameobjects) as the system for detecting placement of blocks. This kind tile data is better of handled as pure data (1d array, 2d array). $$anonymous$$anage and check your tile placement in array data, then "render" it using gameobjects. Yes, gameobjects should be used for items like rolling ball, stackable boxes, things that exist in 3d world, but they are not very suitable for task like this.
Oh that makes sense, thanks. Can you provide a simple example out of this? $$anonymous$$ost documentations I read online are really so advanced like I don't even understand what I'm reading.
@NoDumbQuestion already provided some links, like I said. You should familiarize yourself with the concept first.
Answer by eses · Aug 15, 2018 at 08:43 AM
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];
}
Thank you so much! I kinda get the idea. I tried this code on another scene just to mess with it and this might be the one to help me! I just wonder, what do the "Tile at 0,0 is at: 1" is for? Like I don't get it.