Find a child with a name, how to??

Hello there! I just wanna know if there is a function that returns if there is an object in my hierarchy by checking its name or tag.

situation:

My character have 3 kinds of object to pickup, so I wanna check the name or tag of his hand child, how can I do it??

This can be solved as a one off for all transforms by using an extension method. It also doesn’t require you to pass in redundant parent info.

public static class Extensions
{
    public static Transform Search(this Transform target, string name)
    {
        if (target.name == name) return target;

        for (int i = 0; i < target.childCount; ++i)
        {
            var result = Search(target.GetChild(i), name);
            
            if (result != null) return result;
        }

        return null;
    }
}

Usage:

var foo = transform.Search("Foo");

transform.Find();

transform.Find(); will only search the immediate children. To search a whole hierarchy (children of children) for a transform of particular name - call a function like this (.js)

static function FindTransform(parent : Transform, name : String) : Transform
{
    if (parent.name == name) return parent;
    var transforms = parent.GetComponentsInChildren(Transform);
    for (var t : Transform in transforms)
    {
        if (t.name == name) return t;
    }
    return null;
}

Andrew 10 – great answer! Thanks. I have taken your code and adapted it (JS) to use tags

// FIND CHILD WITH TAG
function findChildWithTag(tagToFind:String) {
	return findChildWithTag(tagToFind, this.gameObject);
}
function findChildWithTag(tagToFind:String, startingObject:GameObject) {

	var childTransforms:Component[]		= startingObject.GetComponentsInChildren(Transform);
	for (var thisComponent:Component in childTransforms) { 
		var thisTransform:Transform		= thisComponent as Transform;
		if (thisTransform.gameObject.tag == tagToFind) {
			return thisTransform.gameObject as GameObject;
		}
	}
	return null;
}

Thank you Andrew 10 for your answer.

I just used to selectively turn off a few buttons in a virtual gamepad implementation:

//Hide all buttons' active/pressed state.
function SetupVirtualPad() {
	var transforms = gameObject.GetComponentsInChildren.<Transform>();
    for (var t : Transform in transforms)
    {
        if (t.gameObject.name.Contains("On") && t.gameObject.GetComponent.<Renderer>() != null) {
        	t.gameObject.renderer.enabled =  false;
        }
    }
},