Use GetComponentsInChildren but don't access grand children

I have a script and it uses the GetComponentsInChildren function on a game object but when the function is executed, it returns all the children including the grandchildren.
111562-getchildquestion.png

How do I make the GetComponentsInChildren function only access the first children in yellow and not access the grandchildren?

I wrote that script for you on the fly. Attach it to your “Parent” gameobject and check whether it works as you want. I created the “GetComponentsInDirectChildren” extension method which returns the list of desired components in direct children (in gameobjects you marked in yellow). Additionally, I created “GetComponentsRecursive” extension method which returns the list of desired components in direct children and the parent. Here’s the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Raja : MonoBehaviour
{
	private void Awake()
	{
		var children = gameObject.GetComponentsInDirectChildren<Transform>();
		
		foreach (var child in children)
		{
			Debug.Log(child.name);
		}
	}
}

public static class Extensions
{
	public static List<T> GetComponentsRecursive<T>(this GameObject gameObject) where T : Component
	{
		int length = gameObject.transform.childCount;
		List<T> components = new List<T>(length + 1);
		T comp = gameObject.transform.GetComponent<T>();
		if (comp != null) components.Add(comp);
		for (int i = 0; i < length; i++)
		{
			comp = gameObject.transform.GetChild(i).GetComponent<T>();
			if (comp != null) components.Add(comp);
		}
		return components;
	}
	
	public static List<T> GetComponentsInDirectChildren<T>(this GameObject gameObject) where T : Component
	{
		int length = gameObject.transform.childCount;
		List<T> components = new List<T>(length);
		for (int i = 0; i < length; i++)
		{
			T comp = gameObject.transform.GetChild(i).GetComponent<T>();
			if (comp != null) components.Add(comp);
		}
		return components;
	}
}

With System.Linq you can do:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Linq;
 
 public class MyClass : MonoBehaviour{

 Component[] directChildren;

 void Start{
   directChildren =  (from directChild in GameObject.Find("Parent").transform.
                      GetComponentsInChildren<Component>() 
                      where directChild.transform.name == "Child" 
                      select directChild).ToArray();
          }
}

Or if you don’t want to rely on the naming:

    public GameObject parentGO; // drag into field in editor

    Component[] directChildren =  (from directChild in parentGO.
                                   GetComponentsInChildren<Component>() 
                                   where directChild.transform.parent == parentGO.transform 
                                   select directChild).ToArray();

Or if you don’t want to use Linq:

List<Component> directChildren = new List<Component>();
foreach(Transform go in parentGO.transform){  // This will only find direct children
       Component c = go.gameObject.GetComponent<Component>();
       directChildren.Add(c);
}