Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by Josh 14 · Aug 09, 2011 at 01:10 AM · collisiontagoncollisionentergameobject.tag

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()?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Aug 09, 2011 at 01:39 AM 4
Share

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.

avatar image Peter G · Aug 09, 2011 at 03:48 AM 2
Share

Another note: using gameObject.CompareTag("Tag") allocates less memory than gameObject.tag == "tag".

5 Replies

· Add your reply
  • Sort: 
avatar image
22

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by timberap · Jul 17, 2017 at 04:24 AM

just use if(other.tag == "Tag")

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Josh 14 · Aug 19, 2011 at 02:37 AM 1
Share

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.

avatar image Bovine · Aug 19, 2011 at 03:56 AM 1
Share

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.

avatar image
0

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); }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking if Player object is out of bounds 1 Answer

Ammo crate collision 2 Answers

Colliding with the tag of a Child Object 2 Answers

On Trigger Enter, Collide with object, specific collision 1 Answer

Collide detection with tag [JS] 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges