Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by uanmanarmy · Jul 25, 2014 at 11:06 AM · gameobjectparentchildchild-to-gameobject

Get Child from a parent

Hello!!, how to access one of the child of the parents.

alt text

I want to access bslider who is child of Item_bbg. Then I will access components of the bslider, as Transform.

Why not putin this item in an public GamObject and access it on start?

Because I'm instantiating the prefabs, and they are different (number of 6 different), but the bslider is the same to every of them. That's why I can't access it by tag or by Finding the name on start.

All I did for now is to create a function that Find all of the childrens and to find the one with the specific name .

 public Transform FindChild( Transform parent, string name, SpriteRenderer _energyBarSpriteRenderer)
 {
     if (parent.name == name)
                     return parent;
     Transform pTransform = parent.GetComponent<Transform> ();
     foreach (Transform t in pTransform)
             {
                 if (t.name == name) {
                     _energyBarSpriteRenderer = t.GetComponent<SpriteRenderer>();

             print ("JORA ASTAAA EU!!!");}
             }
     return null;
 }

But then I don't know how to make an object or how to access this children and it's components. I' find him, but how to access it and to use it I don't know (.

Access and use out of the created function.

безымянный.png (36.0 kB)
Comment
Add comment · Show 3
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 gjf · Jul 25, 2014 at 11:33 AM 0
Share

why not rename them when you Instantiate then you can search for the names in Awake()/Start() and get the required components?

avatar image uanmanarmy · Jul 25, 2014 at 12:04 PM 0
Share

I didnot understand your message,

avatar image gjf · Jul 25, 2014 at 01:19 PM 0
Share

when you Instantiate, you'd do something like this:

 private int _numberOfClones = 6;
 
 void Awake()
 {
     for (var i = 0; i < _numberOfClones; i++)
     {
         var clone = Instantiate(blah, blah..) as GameObject;
         clone.name = "Clone" + i;
     }
 }

then to find a specific game object, you would

 GameObject.Find("Clone1");


3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Paulo-Henrique025 · Jul 25, 2014 at 11:27 AM

If the bslider has always the same name you can do:

 Transform t = transform.Find("bslider"); // Search only in childs

And then do whatever you want with it.

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 SENSBoD · Jul 25, 2014 at 11:33 AM

You can use Find, like in previous post or GetChild to get child by index.

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 SilentSin · Jul 25, 2014 at 12:26 PM

I'm not exactly sure what you want, so here are a few possibilities:

 Transform bslider = transform.Find("bslider");

Would get you the transform of the child.

 SpriteRenderer bsliderSpriteRenderer = GetComponentInChildren<SpriteRenderer();

Would get you the sprite renderer. Though if other children also have sprite renderers, then you shouldn't use this because you don't know which one it will give you.

Also, this line is useless:

 Transform pTransform = parent.GetComponent<Transform> ();

"parent" is already a transform, calling GetComponent() on it will just return a copy of the exact same thing. All you would need on the line below is:

 foreach (Transform t in parent)
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

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

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

Related Questions

Make a simple tree 1 Answer

Display Name Above Object 2 Answers

How to check parent value instead name value(more details in post) 3 Answers

Target child of child without knowing name? 1 Answer

All encompassing Game Object. 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