- Home /
How can I get the tag of a child object?
My script is sending a raycast message and I wanted my raycast to be able to detect what object it hit. it seems to be able to detect the gameObject with the tag "Body" easily because it isn't a child object, however the component I'm trying to find below that is a child of other child objects so I'm not really sure how to reference it. I know I can use hit.transform.Find("path/etc"), but that doesn't get the tag. Here's my script if it helps.
 void Raycast()
     {
         Ray rayDirection = Camera.main.ScreenPointToRay(new Vector3((Screen.width / 2),(Screen.height / 2)));
         {
             RaycastHit hit;
             {
                 if(Physics.Raycast (rayDirection, out hit))
                 {
                     Distance = hit.distance;
                     {
                         if (animation.IsPlaying ("Fire"))
                         {
                             if (hit.transform.tag == "Body")
                             {
                                 print ("bodyhit");
                                 hit.transform.SendMessage ("Bodyhit", SendMessageOptions.DontRequireReceiver);
                                 Debug.DrawLine(transform.position, hit.point, Color.red);
                             }
 
                             if (hit.transform.GetComponentsInChildren) //I'm not sure what to do here
                             {
                                 Damage += 75;
                                 print ("Headhit");
                                 hit.transform.SendMessage ("Bodyhit", SendMessageOptions.DontRequireReceiver);
                                 Debug.DrawLine(transform.position, hit.point, Color.yellow);
                                 Damage -= 75;
                             }
                         }
                     }
                 }
             }
         }
     }
That gives me this error I'm not really sure how to fix it. I kind of feel stupid for not even realising that I could do that either.
NullReferenceException: Object reference not set to an instance of an object wfa4.Raycast () (at Assets/Scripts/wfa4.cs:79) wfa4.Firing () (at Assets/Scripts/wfa4.cs:39) wfa4.Update () (at Assets/Scripts/wfa4.cs:26)
That suggests that whatever you hit doesn't have a "path/etc" child.
Yeah, that means I can't miss my target so I suppose this whole thing won't work. :/
I suppose
 if (hit.transform.Find ("Tyrantprefab/Armature/all/base/Bone_001/Bone_003/Bone_004/Bone_005/Capsulehead").tag == ("Head"))
Doesn't work the same way as
 if (hit.transform.tag == "Body")
Answer by Crumpet · Jul 01, 2014 at 02:54 PM
 if (hit.collider.tag == "tagname")
This works perfectly regardless of the fact that it is a child object. Thanks for the help anyway tanoshimi.
Your answer
 
 
             Follow this Question
Related Questions
How can I differentiate between colliding with an object or its child? 1 Answer
Getting a tag from a child 1 Answer
Raycast Collider Tag not returning correct result 1 Answer
How to get the childrens tag from the parent GameObject? 1 Answer
Get a raycast to hit only child objects to use axes 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                