- Home /
Can't Return the Tag of a Child Object
I am trying to get the tag of a child object on my player through OnCollisionEnter. But it actually returns the tag of the parent object, even though there is a separate collider and tag. How do I get around this? Or is this a problem with Unity itself? I have noticed some irregularities with Unity 5 lately. Thanks!
The code I am using is plain and simple: print (col.gameObject.tag);
Answer by Major · Apr 20, 2015 at 06:54 PM
I don't know what it was but it seems to be working now... It seems like there is some very funky stuff happening in Unity soo.... yeah.
Answer by amitDklein · Aug 19, 2021 at 07:17 PM
I just found the solution,
instead of using col.gameObject.tag
try using col.collider.gameObject.transform.tag
Answer by USMC_Prog · Apr 18, 2015 at 05:27 PM
Hello friend. You need the following code to get the tag of the child.
Col.gameObject.Tranform.GetChild("nameofchild");
Hope this helps.
Answer by nockieboy · Apr 18, 2015 at 05:25 PM
All you're doing is printing the tag of the object triggering the collision in the code you've quoted - does the parent have a collider that is triggering the code? In the case you've quoted, however, you could just use col.transform.child.tag
(pseudo code - I'm not in front of Unity to test it.)
the script is attached to a bullet object, when I do try and print the tag of the object collided, it prints the tag of the parent. Not the actual thing it collided with. The child object has a collider, and separate tag. And I think that line works in javascript but not in c#.