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
3
Question by gamedevelopingboy · Jul 09, 2014 at 09:38 AM · editor-scripting

How to use GetComponentsInChildren ( Example :- to print all children gameobject'sname )

i started learning editor scripting....i streaked while my experimentation. kindly anyone help me to understand GetComponentsInChildren.

my project structure is like this

alt text

i want to print the names of all child(Top to Bottom) one by one...including Bottom (inactive child)

my code attached to Top is

public class Example : MonoBehaviour {

 public GameObject[] obj;
 void Awake(){
     obj = GameObject.GetComponentsInChildren<GameObject>();
     foreach(GameObject ob in obj)
     {
         Debug.Log(ob.name);
     }
 }

}

i tried and failed with this code...It will be very helpfull if anyone help me to get it :)

getcomchild.jpg (12.4 kB)
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

3 Replies

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

Answer by gjf · Jul 09, 2014 at 10:05 AM

look for the Transform component

 using UnityEngine;
 using System.Linq;
 
 public class Example : MonoBehaviour
 {
     public Transform[] obj;
 
     void Awake()
     {
         obj = GetComponentsInChildren<Transform>(true);
 
         foreach (var ob in obj.Where(ob => (ob != transform)))
         {
             Debug.Log(ob.name);
         }
     }
 }

the true in the GetComponents call will get active AND inactive.

Comment
Add comment · Show 5 · 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 gamedevelopingboy · Jul 09, 2014 at 10:29 AM 0
Share

wow it working..thank you so much... :)

avatar image gamedevelopingboy · Jul 09, 2014 at 11:52 AM 0
Share

can u explain how .Where(ob => (ob != transform) work...im new to program$$anonymous$$g...

avatar image gamedevelopingboy · Jul 09, 2014 at 11:53 AM 0
Share

is => stands for lessthan or equal operator there?

avatar image gjf · Jul 09, 2014 at 12:14 PM 0
Share

it's LINQ - the following does the same:

 foreach (var ob in obj)
 {
     if (ob != transform)
     {
         Debug.Log(ob.name)
     }
 }

it checks to see whether the child object is actually the parent since GetComponentsInChildren returns that too.

apologies for complicating things for you.

avatar image gamedevelopingboy · Jul 09, 2014 at 12:31 PM 0
Share

thank you so much!!! :)

avatar image
2

Answer by papabooish · Jul 09, 2014 at 10:30 AM

GameObject isn't a component, it's a container for components, so you can't search for it (as far as I know). Instead as said search for the transform component, since all GameObjects contain a transform (In fact the whole object hierarchy is located on the transform component)

 void Awake() {
     bool includeInactive = true;
     Transform[] ts = GetComponentsInChildren<Transform>(includeInactive);
     foreach(Transform t in ts)
         Debug.Log(t.name);
 }

This should find all Transforms, including the Transform of the GameObject that the script is attached to and print the 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
avatar image
1
Wiki

Answer by ruudlenders · Jul 09, 2014 at 10:30 AM

GetComponentsInChildren isn't the best way to loop through a parent-child structure. GetComponentsInChildren search for specific components, but a game object's name is not part of any component. For your specific example, I would do this:

 void Awake()
 {
     PrintNames(this.transform);
 }
 
 void PrintNames(Transform parent)
 {
     Debug.Log(parent.name);
     
     foreach (var child in transform)
     {
         PrintNames(child);
     }
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How can I change the icon of a script within the Project View? 5 Answers

Edit a inspector variable from editor script.(its not saving) 1 Answer

Updating object on inspector value changes in editor 1 Answer

How to get notification of moving a GameObject in the hierachy when editing the scene? 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