Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by NoGo86 · Nov 12, 2012 at 05:08 AM · collisionchildtagoncollisionenter

Colliding with the tag of a Child Object

INFO.... I have a Empty with A few Cubes in it as my "Block" GameObject w/rigidbody. The Cube's each have six box collider's on each side that are flat and a tag of Y-,Y+,X-,X+,Z-,Z+ depending where the side is in world space.

Question.... I need to find out if the "Block" I'm controlling has collided with a Child tag of another "Block" I Have in the scene.

So this is what I would like if it is possible.

 function OnCollisionEnter(other : Collision)
 {
     if (""other.child.tag"" == "Y-")
     {
         DO SOMETHING;
     }
 }


not sure what to put in the ""other.child.tag"" part

When I use

 function OnCollisionEnter(other : Collision)
 {
     if (other.gameObject.tag == "Y-")
     {
         Do Something;
     }
 }


It does nothing, and I'm guessing because its getting the parents tag not the child's.

UPDATE.....I wanted to clarify my set up. I have a empty with a rigidbody. In that empty I have a cube that has six child colliders as the sides. I need my collider code to be with the empty.

Comment
Add comment · Show 7
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 Seth-Bergman · Nov 12, 2012 at 05:31 AM 0
Share

no, that doesn't stand to reason.. any child object with a collider will be able to be detected individually, regardless of their parent/hierarchy.. $$anonymous$$uch more likely your function is never being entered at all.. forget checking for the right tag, just try this:

 function OnCollisionEnter(other : Collision)
 {
 Debug.Log("HIT! :" + other.gameObject);
 }

my guess is nothing happens.. You might try triggers ins$$anonymous$$d, or otherwise you need to add rigidbodies to the CUBES themselves

here are a few links regarding proper set-up of collisions:

http://unitygems.com/mistakes1/

and here, at the bottom, the collision action matrix:

http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html

EDIT:

On closer inspection of your described set-up, you may be right, I was misunderstanding the "block v block" dynamic.. I would say, test with a debug like I suggest.. if you get the parent, you may want to simply use 6 rigidbodies, one for each collider..

OR maybe try going to the collider, rather than directly to the gameObject:

 function OnCollisionEnter(other : Collision)
 {
 if(other.collider.tag == "Y-")  ... etc

I suspect that may be just crazy enough to work...

avatar image prototype7 · Nov 12, 2012 at 05:40 AM 1
Share

Or you can try to loop your object

 function OnCollisionEnter(coll : Collision){
     for(var c : ContactPoint in coll.contacts){
         if(coll.gameObject.name == "Y-")
             Debug.Log("name " + coll.gameObject.name);
         }
     }
avatar image NoGo86 · Nov 12, 2012 at 01:46 PM 0
Share

thx i will try this when I get home from work and mark it as answered if it does the trick.

avatar image NoGo86 · Nov 12, 2012 at 01:47 PM 0
Share

if my above answer does not work I will try this after work. it my be a few days before I can mark as answered because of work but if its the right one I will mark it. thx

avatar image NoGo86 · Nov 13, 2012 at 04:39 AM 0
Share

This did not work for me. $$anonymous$$aybe because of my set up.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Seth-Bergman · Nov 13, 2012 at 05:17 AM

HERE is my final answer:

first off, to see exactly what collision message is being sent, just try this first:

 function OnCollisionEnter(other : Collision)
 {
 Debug.Log("HIT! :" + other.gameObject);
 }

1: Using your setup, provided the collision is entered at all, you will see that the Debug log gives the name of the PARENT, because that is what has the RIGIDBODY.

2: No problem.. now try this:

 function OnCollisionEnter(other : Collision)
 {
 Debug.Log("HIT! :" + other.gameObject);
 Debug.Log("HIT! Collider: " + other.collider.gameObject);
 }


The second line will give you the name of the ACTUAL OBJECT WITH THE COLLIDER, the child itself.. This works, I just tested it.

If you are not getting any Debug.Log message at all as a result of this test script, you have some other aspect set up wrong of what "OnCollisionEnter" needs to work right.. in which case check out here:

http://unitygems.com/mistakes1/

http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html

Comment
Add comment · Show 4 · 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 hbspatil · Dec 21, 2012 at 06:53 AM 0
Share

Thank you its solved my problem

avatar image Brutang · Nov 25, 2013 at 03:18 PM 0
Share

Thanks! other.collider.gameObject worked great. Now if I throw a spear/knife into a compound collider it becomes the child of the body part, NOT the parent rigidbody like it did before. Just what I wanted!

avatar image osamansr2o1oo · Jun 14, 2018 at 02:31 PM 0
Share

thank u other.collider.CompareTag() worked like a charm :D

avatar image DanikV · Apr 14, 2019 at 09:22 AM 0
Share

Late, but this is exactly what I needed to make child weapon object actually deliver damage, not receive for the parent. Thank you!

avatar image
0

Answer by NoGo86 · Nov 15, 2012 at 06:39 PM

you are right about the not having it set up right. I now have the Main empty with 4 cube objects, each of the cube's have six collision boxes that have rigidebodys with this ontriggerenter

 function OnTriggerEnter (col : Collider)
 { 
     if(col.transform.IsChildOf(transform.parent.parent))
         {
             //Debug.Log (col.gameObject.name);
         }
         else
         {
             Debug.Log (col.gameObject.name + col.gameObject.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

15 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 avatar image avatar image avatar image avatar image

Related Questions

Getting a collision's bottom-most collider in a hierarchy 2 Answers

Ammo crate collision 2 Answers

Detect a gameObject's tag via OnCollisionEnter() 5 Answers

Collision with no contact? 2 Answers

Collision with tag 5 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