Detect a gameObject's tag via OnCollisionEnter()

Hi, I am trying to detect the tag of a gameObject when it collides with another object. Here is my script so far:

function OnCollisionEnter (collision:Collision)
	if (collision.gameObject.tag == "MyTag")
	else if (collision.gameObject.tag == "MyOtherTag")

It compiles and everything, but when I collide with an object with one of those tags I don’t get anything added to the log. Am I missing something or is this just not possible? Thanks in advance for any help!

PS: I got it to work using, but I would much rather use tags instead of names. Thanks again!

EDIT: Hmmm… It seems to work with OnTriggerEnter(other:Collider), but I would still like to know if this can be done with OnCollisionEnter()?

I know this post is 4 years old now, but just to let future questioners maybe find an answer to this:
It seems to work like this:

if(collision.collider.tag == "MyTag")
Debug.Log ("MyTag");

It worked for me.

do this

if(collision.gameObject.CompareTag(“this is your tags name”){



and then same for the on exit

worked for me hope it works for you.

just use if(other.tag == “Tag”)

If it’s working with OnTriggerEnter() then your colliders are checked to be triggers and my understanding is that OnCollisionEnter() won’t be called for a trigger - triggers cause things to happen, colliders, ermmm collide with things.