- Home /
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.
Answer by Kiour_gr · Sep 22, 2020 at 07:01 AM
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;
}
Answer by KarlKarl2000 · Mar 17, 2018 at 10:28 PM
@ 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.
Ooooh even better solution. I missed that part in the documentation. $$anonymous$$uch shorter and sweeter!
Yea its an old thread, but even after 3 years your response was helpful for me. So thanks again for your contributions to the Unity forums!
Answer by Janibasha · Aug 09, 2017 at 09:02 AM
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.
Your answer
Follow this Question
Related Questions
Instantiate problems 1 Answer
Find All FindObjectOfType() 1 Answer
How do I find all game objects with the same name? 7 Answers
How do I find a child object in a hierarchy of children? 1 Answer
Code for built-in Find() function? 1 Answer