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 SirBedlam · Sep 20, 2014 at 12:03 AM · childgameobjectschildrenaccess

How to access child GameObjects (NOT Transforms)?

I've been able to find this question asked all over the place, but for some reason I haven't been able to find a real answer. It's so simple yet has only ever been explained in the vaguest, most cryptic ways possible (at least that I've found).

How do you access child GameObjects?

Not child Transforms, not components of child GameObjects, just plain child GameObjects. By this I mean simply the immediate children, not an extensive hierarchy of them (grandchildren).

I keep seeing this posted everywhere as an answer, but it's not what the question is asking:

 for (var child : Transform in transform)
 {
     child.DoSomething;
 }

I tried adjusting this using "GameObject in gameObject" instead, but it doesn't work. Is there something simple that I'm missing? I can't think of anything I would need to add or change. Forgive me if this is a stupid question, I'm just confused after all the strangely obscure answers I've read.

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

2 Replies

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

Answer by NoseKills · Sep 20, 2014 at 12:07 AM

Class GameObject does not inherit IEnumerable so you can't loop through it with a for-each loop. Transform is an IEnumerable and that's why it can be for-eached.

Luckily you can refer to the connected GameObject through the Transform.

 for (var child : Transform in transform)
 {
     child.gameObject.DoSomething;
 }

Looking at this API document you can see that anything that extends the Component class (basically anything that can be added to a GameObject: MonoBehaviours, Rigidbodies, Colliders) by default has readily made public variables for your convenience for referring to many other components that could exist in that gameObject.

You can refer a GameObject's transform by gameObject.transform or to a Collider of a gameObject through it's gameObject.collider etc... Heck, you can even refer to the gameObject itself by going back and forth as many times as you like if you want to write long and nonsensical code: this.gameObject.transform.gameObject.transform.gameObject.transform.gameObject... :)

Of course, if there's no collider attached to the gameObject, the gameObject.collider will be null

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 SirBedlam · Sep 20, 2014 at 02:16 AM 0
Share

Ah! So it really was something that simple! This makes total sense now, and I feel silly. I always knew about accessing GameObjects' components quickly like this, especially Transforms, so I can't believe I didn't even realize I could be doing it the other way around! That means no more worrying about declaring my Transform/GameObject variables exactly how I need them, and having to make another one for the same object. I guess I've always just seen GameObjects as more of an essential base than they really are. Yours and Bunny83's comments have snapped me out of this. Now, if only I had known this over 2 years ago. XD Thank you.

avatar image
1

Answer by Bunny83 · Sep 20, 2014 at 01:01 AM

The point you have to understand is, that there is nothing like a child-gameobject. A GameObject has no functionality besides:

  • giving the object a name, tag, layer.

  • hold the information if the GameObject is active and if it's static or not.

  • provide a container for components

That's all. The hierarchy you see in the scene view is actually a hierarchy of Transform components. The GameObject class is actually the most useless class in unity as it covers almost no functionality. On the other side it's the most important class as it is required as "glue" between components. In 90% of all cases you don't need to work with GameObject references as most data and functionality is implemented through components.

You only need GameObject references if you want to activate / deactivate an object, destroy a complete gameobject or change it's name, tag, layer.

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 SirBedlam · Sep 20, 2014 at 02:03 AM 0
Share

That last part is in fact exactly what I'm trying to do; activate and deactivate the children. Interesting, though. I didn't even know much of this. Thanks for the enlightenment.

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

26 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

Related Questions

how to access scripts in children 2 Answers

Calling parent variables from child and visa versa 1 Answer

How to change color to every specific material in a child of a group..? 1 Answer

Constraining an object's movement using two spring joints on its children 1 Answer

Cycling Weapons, using C# int string problem 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