Find children in parent

Hello all ,
So this is my question .
I have a game object that contains 4 children .
I want it to get one of those children (can search it by name) ?
how do I do this through code?
Thanks a lot in advance.

Try this: gameobject.transform.Find(“ChildName”) ; on the parent gameobject

In Unity, the hierarchy information is stored in the Transform component rather than the GameObject itself. You can find a child with a given name using the Find method on the Transform:

GameObject GetChildWithName(GameObject obj, string name) {
    Transform trans = obj.transform;
    Transform childTrans = trans. Find(name);
    if (childTrans != null) {
        return childTrans.gameObject;
    } else {
        return null;
    }
}

Place this code on parent object:

foreach (Transform eachChild in transform) {
    if (eachChild.name == "NameWhatYouNeed") {
        Debug.Log ("Child found. Mame: " + eachChild.name);
    }
}

Update: Method above is better. Didn’t know that.

you can use tranform.getchild(index) to get any one of them

GameObject childObject = GameObject.Find(“ParentName”).transform.Find(“ChildName”).gameObject;

Thank you guys, but there is another method which is finding the child transform using the hierarchy index of the child object.

Oh man how it is working @UnityLIb43 , may I know why “transform” is used to find child game object

Hey guys,
gameobject.transform.Find(“ChildName”) ; just works for child of gameobject
for any child for example we wanna get game object in child of child our gameobject like this:
183964-unity-jhp1uyropn.png

our object has many bones I wanna set the LocatorWeapon

public Transform LocatorWeapon;

private void Awake()
    {
        foreach (GameObject obj in FindObjectsOfType(typeof(GameObject)))
        {
            if (obj.transform.IsChildOf(transform) && obj.CompareTag("LocatorWeapon")) LocatorWeapon = obj.transform;
        }
    }

FindObjectsOfType(typeof(GameObject)) get all gameobject in hierarchy.

IsChildOf check if that gameobject in child

this method use for multi object like all units to set specific child.

gameobject.transform.Find(“ChildName”) returns the Transform of your children.

gameobject.transform.Find(“ChildName”).gameObject will return your children gameObjects.

You can use:

GameObject myChild = transform.Find(“ChildName”).gameObject;

It is Transform not transform.