How to detect if an object is colliding with 2 objects that have the same tag?

Let’s say I have a car. When my car collides with 2 cones, I want the cones destroyed. But cones are prefabs and share the same tag. I tried this.

void OnTriggerEnter(Collider collider)
{
  if (collider.gameObject.tag == "cone")
  {
    GameObject cone1 = collider.gameObject;
    if (collider.gameObject.tag == "cone")
    {
      Destroy(cone1);
      Destroy(collider.gameObject);
    }
  }
}

But it seems to destroy cones randomly. I can’t use OnCollision so I have to go with OnTrigger. So what do you think? Thanks.

if collider.gameobject.tag == “cone” && other boundaries here)

First of all, I think your code is confusing because the name of the Collider parameter is collider, which obscures the collider member of the current gameObject. You should be seeing a warning message saying just about that.

On the other hand, the code is redundant, because you are checking the same condition (collider.gameObject.tag == "cone") twice, not doing anything relevant in between. So the second if will always be true. Then, cone1 and collider.gameObject will always be the same, so you are destroying the same object twice.

What you probably want to do is something like:

using UnityEngine;
using System.Collections;

public class CheckTwoCones : MonoBehaviour {
	
	GameObject[] cones = new GameObject[2];
	int collidedWith = 0;
	
	void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.tag == "cone")
		{
			if(collidedWith < 2)
			{
				cones[collidedWith] = other.gameObject;
				collidedWith++;
			}
			
			if(collidedWith == 2)
			{
				Destroy(cones[0]);
				Destroy(cones[1]);
				collidedWith = 0;
			}
		}
	}
}

I haven’t tested it, and it probably needs some adjustement, but it should be about right.