Making objects with the same tag destroy eachother
So for my game I have robots that use a rock-paper-scissors system, where each is better against and weak against each other. So far it's mostly working so far, but I'm having trouble with what happens when two robots with the same tag collided.
Here's the code for one of my robots that tells it how to reacted to the others.
private void OnTriggerEnter(Collider other)
{
if (other.tag == ("Smasher"))
{
Object.Destroy(this.gameObject);
}
if (other.tag == ("Gouger"))
{
Debug.Log("awake");
Object.Destroy(this.gameObject);
}
if (other.tag == ("Wall"))
{
Debug.Log("awake");
Object.Destroy(this.gameObject);
}
}
So far, the collisions with the Gouger and the wall work. But when it collides with another smasher, they just push each other around instead of destroying themselves.
Basically what I'm asking is how can I make two objects that have the same tag destroy each other upon collision?
@Bluesharky38 Note OnTriggerEnter is only called if one of the colliders has a rigidbody and is set to Is Trigger. That may not be the case for you.
Answer by streeetwalker · Mar 07, 2020 at 09:15 PM
You must have something else going on: I took just that function, set up two objects with your tag, and it works perfectly to destroy both.
Put a debug.log( this.name + " > " + other.tag ) into the first line of your OnTrigger, before any of the if statements. Make sure each object has a unique name.
I bet you do not see 2 debug log statements - one for each object colliding - which will mean your objects are not set up the way you expect.
Okay, thanks for the help. I'm not sure how I just got it to work, but thanks for helping me see that the code was okay.
Your answer
![](https://koobas.hobune.stream/wayback/20220612224512im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GameObject not destroying on Collision 1 Answer
How to make it so when i shoot a zombie the has been spawned, it will destroy it? 1 Answer
It seems my tag detection does not work properly 2 Answers
Usage of multiple tags or some other workaround? 1 Answer
My simple 2D object isn't colliding before it gets destroyed 0 Answers