• 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 c$$anonymous$$ld of a gameobject by name?

t$$anonymous$$s is the part of the code:

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

I need to find the c$$anonymous$$ld of a c$$anonymous$$ld gameobject named "Bone", but i cant find it t$$anonymous$$s 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

7 Replies

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

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

You can specify the path name to find a "grandc$$anonymous$$ld", like in the Transform.Find example:

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

But if you have more than one c$$anonymous$$ld with the same name, you can iterate through all c$$anonymous$$ldren comparing the names or tags:

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

In your code, you could also use the c$$anonymous$$ld found to search in its own c$$anonymous$$ldren:

    if ($$anonymous$$t.transform.name == "Bone"){
        target = true;
        var boneC$$anonymous$$ld = $$anonymous$$t.transform.Find("Bone");
        if (boneC$$anonymous$$ld){
            // bone c$$anonymous$$ld 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 getC$$anonymous$$ldGameObject(GameObject fromGameObject, string withName) {
         //Author: Isaac Dart, June-13.
         Transform[] ts = fromGameObject.transform.GetComponentsInC$$anonymous$$ldren();
         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 FindC$$anonymous$$ldByRecursion(t$$anonymous$$s Transform aParent, string aName)
         {
             if (aParent == null) return null;
             var result = aParent.Find(aName);
             if (result != null)
                 return result;
             foreach (Transform c$$anonymous$$ld in aParent)
             {
                 result = c$$anonymous$$ld.FindC$$anonymous$$ldByRecursion(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 = GetComponentsInC$$anonymous$$ldren<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 1
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>  c$$anonymous$$ldrens = new List<GameObject>();
 int count = 0;

 w$$anonymous$$le (count < gameObject.transform.c$$anonymous$$ldCount)
 {
     c$$anonymous$$ldrens.Add(gameObject.transform.GetC$$anonymous$$ld(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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

How to Access Components in Children of GameObject? 1 Answer

Instantiate as child 3 Answers

Can one specify the parent of a gameobject in an array? 2 Answers

[Question] Optimization Editor vs Script? 1 Answer

Using same named childs for Prefab 2 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