Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
3
Question by gdubrocks · Feb 19, 2015 at 08:34 PM · children

How to get children GameObjects

I have a healthbar prefab (GameObject) with three children that I wish to modify.

alt text

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

heiracrchy-2.png (2.5 kB)
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

3 Replies

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

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.

Comment
Add comment · Show 3 · 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 gdubrocks · Feb 19, 2015 at 09:12 PM 1
Share

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";    
         }











avatar image Wolfdog · Feb 20, 2015 at 12:58 PM 0
Share

@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.

avatar image gdubrocks · Feb 20, 2015 at 08:00 PM 0
Share

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?

avatar image
0

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");

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
0

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";



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 unity_HTr7mOeTIRzvVA · Jun 21, 2021 at 08:59 AM 0
Share

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

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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Baking NavMesh at Runtime: Connecting separate NavMeshSurfaces 0 Answers

How to sync children of a gameObject using Photon? 0 Answers

making the instantiated game object as a child of the GameObject 1 Answer

How To Deactivate A Parent (And Its Children)? 4 Answers

I want to make a animation for a gameobject which have children gameobject! 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