• 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
24
Question by Edyvargas · Nov 06, 2011 at 11:03 PM · gameobjectchild

How to find a Child Gameobject by name?

Hi, how can i find a second child of a gameobject by name?

this is the part of the code:

     function Update (){
 
     var ray : Ray = playerCamera.ViewportPointToRay (Vector3(0.5,0.5,0));
     var hit : RaycastHit;
 
     if (Physics.Raycast (ray, hit, distanceDetection)){
 
         if(hit.transform.name == "Bone"){
 
             target = true;
 
         } else {
 
             target = false;    
         }
 
     } else {
 
         target = false;
     }
 }

I need to find the child of a child gameobject named "Bone", but i cant find it this way.

Comment
Add comment
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

6 Replies

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

Answer by aldonaletto · Nov 06, 2011 at 11:25 PM

You can specify the path name to find a "grandchild", like in the Transform.Find example:

   aFinger = transform.Find("LeftShoulder/Arm/Hand/Finger");

But if you have more than one child with the same name, you can iterate through all children comparing the names or tags:

   for (var child in transform){
       if (child.name == "Bone"){
           // the code here is called 
           // for each child named Bone
       }
   }
 

In your code, you could also use the child found to search in its own children:

    if (hit.transform.name == "Bone"){
        target = true;
        var boneChild = hit.transform.Find("Bone");
        if (boneChild){
            // bone child found
        }
    } else {
        ...

Comment
Add comment · Show 5 · 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 Edyvargas · Nov 07, 2011 at 12:11 AM 0
Share

I use the first method, Thank you very much aldonaletto!

All is the same, i only add one line to my code, and edit the name, this are the changes:

 bone = transform.Find("Crate/Crate [Layer1]/Bone"); //new line added, create a var name that holds the child object name.

 if (Physics.Raycast (ray, hit, distanceDetection)){

     if(hit.transform.name == bone){ //insted of use string name, use the variable with the name.
avatar image aldonaletto · Nov 07, 2011 at 12:30 AM 0
Share

The last line will not work, because bone is a Transform and hit.transform.name is a String. You can't compare Transform to String (transform.name), but you can compare Transforms:

 if (hit.transform == bone){
avatar image Edyvargas · Nov 07, 2011 at 12:53 AM 0
Share

You are right, its not a string, but works that way and dont with (hit.transform == bone) without the "name" at the end....i dont know why ¿?... UPDATE: the code isnt working properly, i must tune it some more..

avatar image aldonaletto · Nov 07, 2011 at 10:09 AM 0
Share

You could use:

   if (hit.transform.name == "Bone"){ // compare names

or

   if (hit.transform == bone){ // compare Transforms

Notice that you use the full path name to find some object deep in the hierarchy, but the transform.name property itself holds only the name - it's like files in the computer: the filename is always the same, no matter in which directory it is.
Another thing: maybe you're having problems because the object was not found due to some misspelled name or other reason; place a debug line after Find:

 bone = transform.Find(....);
 if (!bone) print("Bone not found");
avatar image Edyvargas · Nov 07, 2011 at 07:47 PM 0
Share

Ill try that and post the results later, thanks!.

Note: by the moment im using (hit.transform.tag == "Bone"), and put to the grandchild the tag name "Bone", that way the object its called always without any problem, but i want to do it without the need of create a new tag name if possible, post results later...

avatar image
16

Answer by IMD · Jun 12, 2013 at 12:01 PM

Here's a little function I just cooked up that might come in handy..

  static public GameObject getChildGameObject(GameObject fromGameObject, string withName) {
         //Author: Isaac Dart, June-13.
         Transform[] ts = fromGameObject.transform.GetComponentsInChildren();
         foreach (Transform t in ts) if (t.gameObject.name == withName) return t.gameObject;
         return null;
     }
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 FamerJoe · Jan 15, 2014 at 01:38 PM 0
Share

You need to add the class type to this line for this to work:

Transform[] ts = fromGameObject.transform.GetComponentsInChildren();

avatar image Pawl · Jul 18, 2014 at 11:32 PM 2
Share
 Transform[] ts = fromGameObject.transform.GetComponentsInChildren<Transform>(true);
avatar image
3

Answer by vladibo · Nov 16, 2018 at 02:24 AM

         internal static Transform FindChildByRecursion(this Transform aParent, string aName)
         {
             if (aParent == null) return null;
             var result = aParent.Find(aName);
             if (result != null)
                 return result;
             foreach (Transform child in aParent)
             {
                 result = child.FindChildByRecursion(aName);
                 if (result != null)
                     return result;
             }
             return null;
         }
 
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
3

Answer by shawnblais · Aug 15, 2020 at 08:15 AM

Using Linq:

 var allKids = GetComponentsInChildren<Transform>()
 var kid = allKids.Where(k => k.gameObject.name == name).FirstOrDefault();

Comment
Add comment · Show 1 · 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 sammx343 · May 29, 2021 at 05:14 AM 0
Share

I just changed it to:

var kid = allKids.First(k => k.name == name);

avatar image
1

Answer by anthoxico · Nov 15, 2018 at 05:55 PM

 List<GameObject>  childrens = new List<GameObject>();
 int count = 0;

 while (count < gameObject.transform.childCount)
 {
     childrens.Add(gameObject.transform.GetChild(count).gameObject);
     count++;
 }
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
  • 1
  • 2
  • ›

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

13 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

Related Questions

how do I get reference to a parent or a child? 2 Answers

How to check parent value instead name value(more details in post) 3 Answers

Display Name Above Object 2 Answers

Find a specific child of a GameObject with the collision data from OnControllerColliderHit 4 Answers

Activating gameObject issue... 1 Answer


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