• 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
1
Question by Nodgorod · Nov 29, 2015 at 09:56 AM · child object

How do I get my child object if I know his parent??

I have a parent object, whose name is known. But I have a child object, whose name is unknown to me, but the good thing is that there is only one child object. How could I get a reference to that child object knowing his parent details?

Comment
Add comment · Show 1
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 savlon · Nov 29, 2015 at 02:57 PM 0
Share

I'm on my phone but it's something along the lines of "yourparentgamrobject.transform.GetChild(index)" the index is the position of the child game object reading from top to bottom.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by lloladin · Nov 29, 2015 at 04:05 PM

if you just want the parrents child you can do soemthign like

 parrent.transform.getChild(0);

this will get the first child in the parrent object

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 servival · Nov 29, 2015 at 04:43 PM

Very easy. lets say that your object's name is robot.

  public GameObject  Robot;
 
     void Start() {
                 
         RobotPart =  Robot.Transform.GetChild(0);  // try 1 if 0 doesn't work.
         
     }
 
        void Update() {
         
         RobotPart.transform.Rotate(Time.deltaTime*20, 0, 0);
     }
 
 

Comment
Add comment · Show 2 · 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 Bunny83 · Nov 30, 2015 at 05:06 AM 0
Share

It's kind of funny to see 3 answers and all 3 have different errors in the code.

@servival: The GameObject class doesn't have a GetChild method. A GameObject doesn't have childs, only the Transform component has.

@HyperQuantum: GetChild is a method, so you need invokation brackets. It also requires a child index as parameter.

@lloladin: $$anonymous$$ethodNames in C# and Unity are always UpperCamelCase, so it's GetChild(0).

Also be careful with the child index. If you try to access a child that doesn't exist, the GetChild method will throw an exception. You might want to check the childCount before using GetChild.

avatar image HyperQuantum · Nov 30, 2015 at 08:15 AM 0
Share

@Bunny83 Thank you for the correction, oh wise one :D

avatar image
0

Answer by HyperQuantum · Nov 29, 2015 at 04:43 PM

By name: GameObject.Find("parentName").transform.GetChild.gameObject;

By tag: GameObject.FindGameObjectWithTag("parentTag").transform.GetChild.gameObject;

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

9 People are following this question.

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

Can't find Animator State/Invalid Layer Index in child object inside prefab 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