- Home /
How to get children GameObjects
I have a healthbar prefab (GameObject) with three children that I wish to modify.
I have access to this healthbar via a list.
//instantiates the player rings
for(int i = 0; i < playerList.Count; i++){
healthList.Add (Instantiate(healthBar, new Vector3(i*100+50,0,0), Quaternion.identity) as GameObject);
healthList[i].transform.SetParent(healthPanel.transform,false);
GameObject currHealth = //help!!
Text currHealthText = currHealth.GetComponent<Text>();
currHealthText.text = "100";
GameObject currHealth = //help
Text maxHealthText = maxHealth.GetComponent<Text>();
maxHealthText.text = "100";
GameObject ring = //help here!
Image ringImage = ring.GetComponent<Image>();
ringImage.fillAmount = 1;
}
maybe its possible to use this method? http://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html
Answer by Wolfdog · Feb 19, 2015 at 08:46 PM
for (int i = 0; i < YourHealthbarGameObject.transform.childCount - 1; i++) {
if (YourHealthbarGameObject.transform.GetChild(i).transform.name == "Current Health"){
YourHealthbarGameObject.transform.GetChild(i).transform.GetComponent<Text>().text = "100";
}
}
for (int i = 0; i < YourHealthbarGameObject.transform.childCount - 1; i++) {
if (YourHealthbarGameObject.transform.GetChild(i).transform.name == "Ring"){
YourHealthbarGameObject.transform.GetChild(i).transform.GetComponent<Image>().fillAmount = 1;
}
}
You can of course create a function and pass the names of the children as a parameter, but this is the basic idea. You iretate over the children and check if the child has the name you want.
Thanks! This worked great.
I don't really understand why GetChild (not getChild) needs a .transform to be called, but the method works just fine. It's also a bit weird how the children are referenced by index (I like how you added the check by name), but it works.
$$anonymous$$y code ended up being something like:
//instantiates the player rings
for(int i = 0; i < playerList.Count; i++){
healthList.Add (Instantiate(healthBar, new Vector3(i*100+50,0,0), Quaternion.identity) as GameObject);
healthList[i].transform.SetParent(healthPanel.transform,false);
healthList[i].transform.GetChild(0).GetComponent<Image>().fillAmount = 98/100;
healthList[i].transform.GetChild(1).GetComponent<Text>().text = "98";
healthList[i].transform.GetChild(2).GetComponent<Text>().text = "100";
}
@gdubrocks thanks for the correction. The way you did it is fine as long as you know the order that the child objects are in. I usually dynamically parent children to objects, and then check if a parent has a specific child attached to it, so I don't usually know what order the children are (hence I have to check the whole list).
As for the transform, I'm not sure why I do that. I think that it's because sometimes I need it and I don't have it, so ins$$anonymous$$d it's better for me to have it even when I don't need it.
I tried it without the transform and it didn't work, so I think it is needed. I just don't really get why. Can't GameObjects have parents or children unrelated to transforms or is that how children are made?
Answer by rodiri · Apr 04, 2021 at 12:57 AM
I know this is an old one and is already solved. But I had the same question and created a function:
private Transform FindChild(Transform parent, string name)
{
for (int i = 0; i < parent.childCount; i++)
{
Transform t = parent.GetChild(i);
if (t.name == name)
return t;
}
return null;
}
I have these game objects:
Cannon
-Top
-Bottom
-Barrel
-SpawnPoint
-Cube
I can get the objects with:
barrel = FindChild(cannon, "Barrel");
spawnPoint = FindChild(barrel, "SpawnPoint");
Answer by DavidSof · Jun 21, 2021 at 08:43 AM
I find this one works good for me and its almost 2 Line of code
Transform[] healthBarChildrens;
healthBarChildrens= healthBar.GetComponentsInChildren<Transform>();
currHealth = System.Array.Find(healthBarChildrens, p => p.gameObject.name == "Current Health").gameObject.GetComponent<Text>().text = "100";
it is indeed two lines. But it seems like a lot of lines combined. It doesn't need to be short we still make it more readable by caching it into different variables. Other than that it still works great.
Your answer
![](https://koobas.hobune.stream/wayback/20220613183805im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Destroy just the last child from a parent after the child hit a trigger 0 Answers
How can i change the scriptable object variable in child script (if it's possible) 0 Answers
Is there any way to use parenting as an organization tool without creating a Transform bond? 3 Answers
how to disactive or destroy the children 4 Answers
Find children and set them all to be kinematic rigidbodies 1 Answer