Unity C# - Target a Game Object (with Tag) on a Specific Vector2 Location
To start with, I have this grid:
| C1 | C2 | C3 | C4 | C5 |
| B1 | B2 | B3 | B4 | B5 |
| A1 | A2 | A3 | A4 | A5 |
I then have a GameObject with the tags "RedBlock" , "GreenBlock", and "YellowBlock". Now whenever a GreenBlock
enters a specific location for example A3
, I want to destroy that specific block/gameObject only. I was thinking about doing this:
(1) gameObject tagged with "GreenBlock" enters A3 - (2) A3 then reads the tag of that object - (3) if the tag is a "GreenBlock" declare and run a certain function - (4) That function will then "Destroy" a gameObject with a tag of "GreenBlock" that is on the vector2 position of the A3
I managed to reach step (3), but somehow I'm stuck on the step (4) since I don't know how to exactly "point" at the A3's location, check if a gameobject with a specific tag is there, and then delete it.
Any solutions to this? Thanks!
Answer by Demondarem · Aug 15, 2018 at 04:38 PM
There are many ways to do that: - You can assign collisions to those objects. If "Green Block" enters "A3"s collision, A3 checks if it has a specific tag, if it's true, A3 runs the function.
void OnCollisionEnter (Collision other)
You can check if Green Blocks position equals to A3s position, if it's true, A3 runs the function
if(transform.position == GreenBlock.position) { Destroy(GreenBlock); }
You can use the Vector.distance script, if the distance is less than 0.1f or something like that destroy the greenblock