- Home /
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")
{
Debug.Log("MyTag");
}
else if (collision.gameObject.tag == "MyOtherTag")
{
Debug.Log("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 gameObject.name, 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()?
If names work but tags not, it seems you're making some mistake when registering the tags or when tagging the objects. Try to replace the if*s with just Debug.Log(collision.gameObject.tag);* to see what it returns.
Another note: using gameObject.CompareTag("Tag") allocates less memory than gameObject.tag == "tag".
Answer by emillio007 · Jun 21, 2015 at 11:02 AM
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.
Answer by IMTRIGGERHAPPY9 · Aug 19, 2011 at 03:28 AM
do this
if(collision.gameObject.CompareTag("this is your tags name"){
Debug.Log(collision.tag)
}
and then same for the on exit
worked for me hope it works for you.
Answer by Bovine · Aug 18, 2011 at 08:47 PM
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.
I had them as regular colliders at first, and then I switched them to triggers and also switched the function I was using, so it's not that. Thanks for answering though.
So it wasn't working as colliders but you switched to triggers and switched the function and it does work?
I suggest you look at the matrix in the advanced section here:
http://unity3d.com/support/documentation/Components/class-BoxCollider.html
Also does at least one of your colliders have a rigidbody attached?
If your colliding and nothing is being output then your method doesn't seem to be being called. If you have it working with names however that suggests that it IS being called so as other people are suggesting you can print() your tag to the console.
Answer by herrindiho · Apr 07, 2020 at 07:44 AM
void OnCollisionEnter(Collision collisionInfo)
{
Debug.Log(collisionInfo.collider.tag);
}
, void OnCollisionEnter(Collision collisionInfo) { Debug.Log(collisionInfo.collider.tag); }
Your answer
Follow this Question
Related Questions
Ammo crate collision 2 Answers
Colliding with the tag of a Child Object 2 Answers
Checking if Player object is out of bounds 1 Answer
Objects in my scene are not responding to OnCollisionEnter 0 Answers
Using collision with Vector3.MoveTowards 3 Answers