GameObject.Find, but only inside of a specific object

I’m sure this has been answwered somewhere, but I couldn’t find it. Basically, I went to find all GameObjects named “food” INSIDE of a specific GameObject. Since gO.Find() defaults to the ENTIRE scene, how can I say something like:

GameObject.Find(“food” inside of AREA ONE)

Just use GetComponentsInChildren, it’s super easy to use, it’s recursive, it’s cool:

// This will get the `Transform` component from all of `areaOne` children, 
// and all children of its children, and all children of its children children,
// and all..., well, you get the idea...
var children = areaOne.GetComponentsInChildren<Transform>();
foreach (var child in children)
      if (child.name == whatYoureLookingFor)
           // do something

If you know the name of parent game object then you can use -

GameObject.Find("ParentGameObjectName/food");

How about this?

GameObject childrenGO = parentGO.transform.Find("ChildGOName").gameObject;

It’s way shorter and I’d guess more performant.

#note, “FindChild” is nowadays “Find”

I’ve created a simple extension method that allow you to search a component only inside of the selected gameObject:

public static GameObject FindObjectInChilds(this GameObject gameObject, string gameObjectName)
	{
		Transform[] children = gameObject.GetComponentsInChildren<Transform>(true);
		foreach (Transform item in children) {
			if (item.name == gameObjectName) {
				return item.gameObject;
			}
		}

		return null;
	}

Usage:

this.gameObject.FindObjectInChilds("You object name");