• 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 nTu4Ka · Oct 01, 2015 at 04:58 PM · child object

How to reference a child object?

Hi,

I have a UI Button and have a reference to it. It has two children. Can I reference any of them by using their parent reference?

I found what seems to be solution but it looks weird - it suggests to use transform.Find() of the parent object.

Comment
Add comment · Show 2
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 Arycama · Oct 01, 2015 at 05:07 PM 0
Share

What did you want to accomplish on the child objects? if it is something like setting the text of a child object, the easiest way would just be to have a public Text object field which you then set in the inspector by dragging and dropping the child. You'd only need something more advanced if you expect the number of children to change at runtime.

avatar image nTu4Ka · Oct 01, 2015 at 05:21 PM 0
Share

@Arycama I need to set alpha of a child object. Buttons are created at runtime from prefabs. So setting it up in editor is not an option. Problem also is that there are two child objects and possibly will be more.

3 Replies

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

Answer by Jessespike · Oct 01, 2015 at 05:03 PM

  Transform[] childTransforms = parent.GetComponentsInChildren<Transform>();
Comment
Add comment · Show 4 · 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 nTu4Ka · Oct 01, 2015 at 05:22 PM 0
Share

It won't help. I need to reference a distinct child. There is a Button parent and two Image children.

avatar image Jessespike · Oct 01, 2015 at 05:25 PM 0
Share

$$anonymous$$ake a script for the child references?

 using UnityEngine;
 public class ScriptForParent : $$anonymous$$onoBehaviour {
     public GameObject[] childReferences;
 }
 

avatar image nTu4Ka · Oct 01, 2015 at 05:31 PM 0
Share

@jessespike It's a bit more complicated in my project. I get reference to parent from hover event. And need to affect child from that point.

avatar image nTu4Ka · Oct 02, 2015 at 06:34 PM 0
Share

@jessespike Actually this may be the best answer. Did that by "GameObject ChildObject = ParentObject.transform.Find("InstantitatedObject(Clone)").gameObject;" As far as I understood GetComponentsInChildren is good alternative way.

avatar image
0

Answer by nTu4Ka · Oct 01, 2015 at 05:32 PM

Done it with transform. It works. But it still looks weird for me and seems laggy.

 ParentObject.transform.GetChild(1).GetComponent<CanvasRenderer>()


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 Luc-Duong · Oct 01, 2015 at 06:07 PM

can use : GameObject[] gameObject = GameObject.FindObjectsOfTypeAll (typeof(GameObject)) as GameObject[]; then find by it's name

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Instantiate at Child Object 2 Answers

how to find the child of an object i hit with raycast 1 Answer

Moving certain sprites in my animation causes weird slingshot effect 0 Answers

can i rotate a child object around the roots local axis using the childobjects reference point? 0 Answers

How do I reference the cameraEye in another script 0 Answers


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