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.

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: Understanding LINQ (C#) - CodeProject, 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
    }
}

This should do the trick :wink:

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;
	}

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>()

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.

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++)
            {
               ...
            }