How Can I find object Child's Child or Child in child with name ?

Dear Sir/Madam

**How Can I find object Child’s Child or Child in child with name ?**please see the picture.

[36776-child’s+child.png|36776]

5 Answers

5

I know this post is ancient, but when I had the same problem this link came up on Google so chill.
From what I read still in 2020 there is no way to search by name all of the children’s children bellow the parent.

What I did was make a static function to do the above for me. I am replying to an ancient post cause maybe someone else finds it useful.

public static GameObject descendant = null;

public static GameObject ReturnDecendantOfParent(GameObject parent, string descendantName)
    {
     
            foreach (Transform child in parent.transform)
            {                   
                if (child.name == descendantName)
               { 
                descendant = child.gameObject;                 
                break;
                }
                else
                {
                    ReturnDecendantOfParent(child.gameObject, descendantName);
                }                  
            }
        return descendant;
    }

@ Reaksmey-Rt
transform.Find is a god send. [Edit] Read Bunny’s useful top link for more clarification on use.

@ Janibasha had a brute force way to code it, which I used to use. But I found another way that I like better.

If we assume Hero1 is the top most game object. Also we assume your script is on Hero1.

   public GameObject _hero1; //assuming these are gameobjects
   public GameObject _use; //assuming these are gameobjects

   Void Start ()
   {
       _use = _hero1.transform.Find("Information").transform.Find("Use").gameobject;
   }

Hope this helps others that find this thread.

sharin’ is carin’

@indieDoroid

This is what i answered 3 years ago. Also if you actually read the documentation page i've linked in my answer you will notice that you can use a "path-like" string. So you can simply do: _use = _hero1.transform.Find("Information/Use").gameobject; The documentation does mention this in the description and actually has a code example as well. However i don't think the OP cares anymore since he wasn't seen here since nov. 2016 and this question is already 3+ years old.

for me that did the trick:

GetComponentInChildren<>()

First Get the game object Hero1 like below

Gameobject hero1 = GameObject.Find(“Hero1”);

then

Transform Cancel = Hero1.transform.GetChild(0); if u want Use chnge index as 1

Cancel .transform.GetChild(0).gameObject.GetComponent().color = new Color(126, 80, 80, 255);

thats it…

Don't wake up old posts, this was already answered.