GetComponentInChildren Returning a Component in Parent

I’m having a problem where GetComponentInChild actually is finding the component in the parent.

My hierarchy is more/less like so (and the script I’m testing this with is below):
Parent (with UISprite)
-child 1 (no UISprite component)
-child 2 (with UISprite)

	void Awake ()
	{
		uiSprite = gameObject.GetComponentInChildren<UISprite>();
		uiSprite.name = "should be child";
	}

I am wondering if I am using this method incorrectly, or if this is the intended behavior? Any advice is appreciated

Simpler way I found:

void Awake ()
    {
        uiSprite = gameObject.GetComponentsInChildren<UISprite>()[1]; //it "jumps" the parent and gives you the child component :D

        uiSprite.name = "should be child";
    }

Since GetComponentInChildren() searches the parent as well, and returns only the first component it finds, it will return the parent’s component, if the parent has a UISprite attached. Therefore, the solution is to use GetComponentsInChildren() to get an array of all UISprite components found in parent and children, and then iterate through the array to find the one you want:

void Awake ()
{

	UISprite[] uiSprites = gameObject.GetComponentsInChildren<UISprite>();
        
  	foreach(UISprite uiSprite in uiSprites)
  	{
      	if(uiSprite.gameObject.transform.parent != null)
          {
          	uiSprite.gameObject.name = "should be child"; //this gameObject is a child, because its transform.parent is not null
          }
  	}
}

was just searching for the same .

with linq - if you dont want to include the parentsComponent.

using System.Linq;
var childrenList = this.GetComponentsInChildren<RectTransform>(true).Where(x => x.gameObject.transform.parent != transform.parent).ToList();

or even better , use neuecc’s exelente linq to GameObject.

using Unity.Linq; //https://github.com/neuecc/LINQ-to-GameObject-for-Unity

var Markers = GameObject.Find("Container").Children().OfComponent<Transform>().ToList();

You might also consider this:

uiSprite = transform.Find("Child Name").GetComponent<UISprite>();