- Home /
Using parent tag on whole gameObject
So I've been trying to use the hitInfo inside some OnCollisionEnter function and I want to take advantage of the tag the gameObject has:
void OnCollisionEnter (Collision hitInfo) {
if (hitInfo.gameObject.tag == "Pad") {
Debug.Log("Pad HIT! " + hitInfo.gameObject.name);
}
}
This won't work! The gameObject tagged as "Pad" has lots of child objects that remain untagged. However if I try this with having the top child object tagged it works. From my understanding what is happening is that when the collision with the Pad happens Unity doesn't see it as a collision with the whole Pad object but with one of its untagged children. Now that's a problem because I do not want to start tagging the children I just want to have the whole parent object tagged since that's what the rest of the environment has to interact with. How do I solve this?
EDIT: This also messes up when I try to use gameObject.name since it won't recognize the gameObject. It gives me a NullReferenceException
Answer by steakpinball · Nov 16, 2013 at 01:55 AM
Unity sees collisions between colliders or rigidbodies. When two colliders hit a message is sent to those specific objects. If that object or a parent of that object has a Rigidbody
then physics messages are sent to those objects instead. OnCollisionEnter
is one of those physics messages. hitInfo.gameObject
is the game object it collided with which has the Rigidbody
attached, or the collider being hit if no RigidBody
is attached.
The solution is to either apply the tag to the object with the Rigidbody
or put the Rigidbody
(or Collider
if the Rigidbody
is on the other object) on the object tagged "Pad".
Your answer
Follow this Question
Related Questions
How to check parent value instead name value(more details in post) 3 Answers
Change gameobject tag when player collides with another gameobject 1 Answer
Target child of child without knowing name? 1 Answer
Game object inheriting children colliders 1 Answer
Why does children position offset on parent collision? 1 Answer