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 Amnoon · Jul 20, 2013 at 05:42 PM · c#gameobjecttransformarray

How can i get ONLY the childrens of a GameOnbject with GetComponentsInChildren method?

Hi. I am using this sentence to get the Childrens of the GameObject who has the script. SceneObjects is an array of Transforms.

  SceneObjects = this.gameObject.GetComponentsInChildren<Transform>(); 

The function works well but i get back all the childrens and the father too. Mamely, i get back this "this.gameObject" too and i don't want this. How can i get only the childrens?

Thanks for your help.

Comment
Add comment · Show 2
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 robertbu · Jul 20, 2013 at 05:45 PM 0
Share

If you want just the immediate children, take a look at the bit of script at the top of the Transform reference page. If you need it in array form, you will have to populate the array yourself, but Transform.childCount gives you the size to make the array.

You could also use your original solution and just set the transform of the this.gameObject to null. Your receiving code could then avoid processing a null reference.

avatar image Benproductions1 · Jul 21, 2013 at 11:55 PM 1
Share

@robertbu You don't specifically have to populate it yourself. You can cast it to an enumerator, cast that to a generic enumerator and then cast that to a list...

@Amnoon Can't you just check whether a object (from the list) is the parent when doing anything?

5 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Noctys · Feb 21, 2015 at 11:42 PM

personally I would add using System.Linq; and then do

 SceneObjects = this.gameObject.GetComponentsInChildren<Transform>().Where(go => go.gameObject != this.gameObject);

Using System.Linq gives you access to the Where extension and then you can run a search. You can read more about Linq here: http://www.codeproject.com/Articles/19154/Understanding-LINQ-C, but you are basically running a search on everything returned by GetComponentsInChildren and only adding it if the search is true.

Although, it would be more efficient to use your code without Linq, and then run your loop like this:

 foreach(Transform t in SceneObjects)
 {
     if(t != this.gameObject.Transform)
     {
         //... Your code
     }
 }
 
 


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 Riderfan · Nov 20, 2017 at 02:07 AM 1
Share

The problem with LINQ is that it's not available on all platforms.

avatar image
4

Answer by JoshuaMcKenzie · Jan 20, 2016 at 07:06 PM

Create an extension Class

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public static class UtilityExtensions
 {
     public static T[] GetComponentsOnlyInChildren<T>(this MonoBehaviour script) where T:class
     {
         List<T> group = new List<T>();
 
         //collect only if its an interface or a Component
         if (typeof(T).IsInterface 
          || typeof(T).IsSubclassOf(typeof(Component)) 
          || typeof(T) == typeof(Component)) 
         {
             foreach (Transform child in script.transform) 
             {
                 group.AddRange (child.GetComponentsInChildren<T> ());
             }
         }
         
         return group.ToArray ();
     }
 }
 

Then all of your Monobehavior scripts can use it and not only for Transforms, but for any type of component or interface. (yes GetComponent can also get Interfaces!)

  SceneObjects = GetComponentsOnlyInChildren<Transform>(); 

if an invalid type is passed in or if there aren't any of that type in the children, it will return an empty array.

EDIT: I missed the part about only direct children (excluding any further descendants). in that case the code for that isn't that much different from what I already wrote, just swap out the GetComponentsInChildren<T>() with GetComponents<T>()

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 Fattie · Jan 20, 2016 at 08:05 PM 0
Share

You know, it's probably easier just to use GetComponentsInChildren and then

      if ( crt == rt ) continue;
      if ( crt.parent != rt ) continue;

to GetComponentsInDirectChildren

avatar image
3

Answer by Gab_RiS · Feb 21, 2015 at 11:27 PM

This should do the trick ;)

 private Transform[] getFirstChildren(Transform parent){
         Transform[] children = parent.GetComponentsInChildren<Transform>();
         Transform[] firstChildren = new Transform[parent.childCount];
         int index = 0;
         foreach (Transform child in children){
             if (child.parent == parent){
                 firstChildren[index] = child;
                 index++;
             }
         }
         return firstChildren;
     }
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 Fattie · Jan 20, 2016 at 06:32 PM

Note, a really simple approach is often like this...

 public float BiggestRightOffsetInChildren()
     {
     RectTransform rt = GetComponent<RectTransform>();
     
     float result=0f;
     foreach (RectTransform crt in
       rt.GetComponentsInChildren<RectTransform>() )
         {
         if ( crt == rt ) continue;
         if ( crt.parent != rt ) continue;
         
         float om = crt.offsetMax.x;
         Debug.Log("    tried " +om);
         if ( om > result ) result = om;
         }
     
     return result;
     }

As you can see the first line just skips "itself" and the second line skips all the non-immediate children.

Just include those two lines ...

         if ( crt == rt ) continue;
         if ( crt.parent != rt ) continue;

... and then do whatever you want with the iteration! Hope it helps

The example there would be used in UI, to find the "most right point" of any of the immediate children.

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 Ng0ns · Dec 13, 2019 at 11:44 AM

AFAIK parent is always at the start of the array, so you could just loop through it with a "for" with (i = 1), thus skipping the first item.

             for (int i = 1; i < length; i++)
             {
                ...
             }
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

22 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

Related Questions

Multiple Cars not working 1 Answer

problem moving a prefab object in script (c#) 0 Answers

How to transform gameObjects in array? 1 Answer

Keep adding targets to a list 2 Answers

Rotating a Vector3 in Instantiation 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