Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
0
Question by Quest_for_stuff · Apr 22, 2020 at 02:22 AM · transformgetcomponentinchildren

Accessing the Transform of a child GameObject of my Parent GameObject?

Hello,

When I wanted to access the transform of a child (a gameobject) of a parent (a gameobject) that was my first intuition : gameObject.GetComponent().GetComponentInChildren().transform; But actually, the solution I found after messing around was GetComponent().GetChild(0).transform; I understand why this solution is working, but I don't understand why my first idea is not working, and I think I would understand a lot if i understand this part. For example, I don't understand either why gameObject.GetComponentInChildren().transform; would also not work. When I do this, it get's the parent transform and not the child transform and I'm really confuse why. My parent Gameobject have only one child with one transform.

Thank you!

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

1 Reply

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

Answer by CiberX15 · Apr 22, 2020 at 02:40 AM

So GetComponentInChildren() is actually slightly misleading. It actually returns the matching component in the transform hierarchy INCLUDING the game object it was called on. So since all game objects have a transform, it will always return the transform of the object you called it on.

This means GetComponentInChildren() is only useful when you are dealing with a component that doesn't exist on the root object.

If, like with transforms, the component you are looking for exists on the parent and the child you can use GetComponentsInChildren() (note the s) like this to ensure you are getting the child component:

 Transform[] transforms = gameObject.GetComponentsInChildren<Transform>();
  for (int i = 0; i < transforms.Length; i++)
 {
         if(transforms[i].gameObject != gameObject)
         {
                 //This is a child transform
         }
 }
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 Quest_for_stuff · Apr 22, 2020 at 02:46 AM 0
Share

Okay wow, thank you a lot!

Also, so is GetComponent().GetChild(0).transform pretty much the best way if I know at what index my component is? And what would I do if I didn't know the index, the script you just wrote would be the best?

avatar image CiberX15 Quest_for_stuff · Apr 22, 2020 at 03:35 PM 1
Share

Yep. If you know the index GetChild(index) should be the fastest way to get there. And as far as I know the for loop is the fastest way if you can't guarantee the order.

If you find yourself having to run the for loop on every frame or on a bunch of objects at the same time and start seeing performance issues, you could run it once and store the result in a variable that way you only have to look it up once. Something like:

 //At the top of your script 
 private GameObject Child = null;
 
 //in the function where you need the child
 if(Child == null)
 {
          Transform[] transforms = gameObject.GetComponentsInChildren<Transform>();
          for (int i = 0; i < transforms.Length; i++)
          {
                   if(transforms[i].gameObject != gameObject)
                   {
                            Child = transforms[i].gameObject;
                            break; //break here since we only need one so skip the rest of the loop
                   }
           }
 }

That has the advantage to that if you later destroy the child and add a new one, the code will automatically detect that Child is null again and grab the new one.

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

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

Related Questions

How to get first depth child components of a parent object? 7 Answers

Problem with GetComponentinChildren and Instantiate 0 Answers

When I set spawn point to a transform, when I play it changes 1 Answer

How to i get value of Rotation.Z from the transform component? 1 Answer

get transform local axis 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