I do a 2D game and I want to understand why the first script work and why my second script doesn't work. The name of myprefab is : "laserRay" and the tag of my prefab is also "laserRay".

void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.CompareTag (“laserRay”))
Destroy (gameObject);

}  // It doesn't work !

void OnTriggerEnter2D (Collider2D co)
{
	if (co.name == "laserRay")
		Destroy (gameObject); 
}   // That code works

You sure the prefab tag is laserRay?

why don’t you debug it?

void OnTriggerEnter2D (Collider2D other)
 {
     Debug.Log(other.name);
     Debug.Log(other.gameObject.tag);
     Debug.Log(other.gameObject.name);
     if (other.gameObject.CompareTag ("laserRay")) 
         Destroy (gameObject);
     
 }

Thank you for your answer.

In fact, other.name was : “laserRay(Clone)” because it’s a prefab.

The correct code should be :

if (co.name == "laserRay(Clone)") 
		Destroy (gameObject); 

Thanks again for your help !