• 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
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
rosdi
erebel55
miladekramnia

People who like this

3 Show 0
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
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
rosdi
alemas
AttilaZold
miladekramnia

People who like this

4 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 programming...

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

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
erebel55
Thyasianman

People who like this

2 Show 0 · 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
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
Laniemme

People who like this

1 Show 0 · 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

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 to check if a key is down in editor script 2 Answers

Hide Object in Hierarchy Window 3 Answers

Filtering project window to ONLY selected objects 0 Answers

How can I remove constant errors from the EditorWindow 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