- Home /
The question is answered, right answer was accepted
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??
Answer by Eric5h5 · Jan 06, 2011 at 05:53 AM
transform.Find();
I only want a confirmation about there is or isnt the object in the hierarchy OR an object with a tag, can transform.find return this information?? I have results to access things using transform.Find but not return if there is or not.
Thanks in advance!
I just want something like "If (I am father of somebody called john){ doSomething();}
or just "if there is somebody called john in my game, do something"
Eric is right: "If no child with name can be found, null is returned." You can use that.
Answer by Mark-Davis · May 23, 2012 at 08:34 AM
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");
Thanks to you I read up on Extension $$anonymous$$ethods and man I've been missing out on some cool stuff! I started using them and I love them!!!!
Answer by Andrew 10 · Jan 14, 2011 at 03:08 PM
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;
}
In C# one can use System.Linq. Then all could be done in one line: transform.GetComponentsInChildren().FirstOrDefault(t => t.name == "whats_your_name")
Answer by bladnman · Apr 07, 2012 at 06:06 PM
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;
}
Answer by Panajev · Apr 12, 2012 at 09:36 AM
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;
}
}
},