- Home /
How to find a Child Gameobject by name?
Hi, how can i find a second child of a gameobject by name?
this is the part of the code:
function Update (){
var ray : Ray = playerCamera.ViewportPointToRay (Vector3(0.5,0.5,0));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, distanceDetection)){
if(hit.transform.name == "Bone"){
target = true;
} else {
target = false;
}
} else {
target = false;
}
}
I need to find the child of a child gameobject named "Bone", but i cant find it this way.
Answer by aldonaletto · Nov 06, 2011 at 11:25 PM
You can specify the path name to find a "grandchild", like in the Transform.Find example:
aFinger = transform.Find("LeftShoulder/Arm/Hand/Finger");
But if you have more than one child with the same name, you can iterate through all children comparing the names or tags:
for (var child in transform){
if (child.name == "Bone"){
// the code here is called
// for each child named Bone
}
}
In your code, you could also use the child found to search in its own children:
if (hit.transform.name == "Bone"){
target = true;
var boneChild = hit.transform.Find("Bone");
if (boneChild){
// bone child found
}
} else {
...
I use the first method, Thank you very much aldonaletto!
All is the same, i only add one line to my code, and edit the name, this are the changes:
bone = transform.Find("Crate/Crate [Layer1]/Bone"); //new line added, create a var name that holds the child object name.
if (Physics.Raycast (ray, hit, distanceDetection)){
if(hit.transform.name == bone){ //insted of use string name, use the variable with the name.
The last line will not work, because bone is a Transform and hit.transform.name is a String. You can't compare Transform to String (transform.name), but you can compare Transforms:
if (hit.transform == bone){
You are right, its not a string, but works that way and dont with (hit.transform == bone) without the "name" at the end....i dont know why ¿?... UPDATE: the code isnt working properly, i must tune it some more..
You could use:
if (hit.transform.name == "Bone"){ // compare names
or
if (hit.transform == bone){ // compare Transforms
Notice that you use the full path name to find some object deep in the hierarchy, but the transform.name property itself holds only the name - it's like files in the computer: the filename is always the same, no matter in which directory it is.
Another thing: maybe you're having problems because the object was not found due to some misspelled name or other reason; place a debug line after Find:
bone = transform.Find(....);
if (!bone) print("Bone not found");
Ill try that and post the results later, thanks!.
Note: by the moment im using (hit.transform.tag == "Bone"), and put to the grandchild the tag name "Bone", that way the object its called always without any problem, but i want to do it without the need of create a new tag name if possible, post results later...
Answer by IMD · Jun 12, 2013 at 12:01 PM
Here's a little function I just cooked up that might come in handy..
static public GameObject getChildGameObject(GameObject fromGameObject, string withName) {
//Author: Isaac Dart, June-13.
Transform[] ts = fromGameObject.transform.GetComponentsInChildren();
foreach (Transform t in ts) if (t.gameObject.name == withName) return t.gameObject;
return null;
}
You need to add the class type to this line for this to work:
Transform[] ts = fromGameObject.transform.GetComponentsInChildren();
Transform[] ts = fromGameObject.transform.GetComponentsInChildren<Transform>(true);
Answer by vladibo · Nov 16, 2018 at 02:24 AM
internal static Transform FindChildByRecursion(this Transform aParent, string aName)
{
if (aParent == null) return null;
var result = aParent.Find(aName);
if (result != null)
return result;
foreach (Transform child in aParent)
{
result = child.FindChildByRecursion(aName);
if (result != null)
return result;
}
return null;
}
Answer by shawnblais · Aug 15, 2020 at 08:15 AM
Using Linq:
var allKids = GetComponentsInChildren<Transform>()
var kid = allKids.Where(k => k.gameObject.name == name).FirstOrDefault();
I just changed it to:
var kid = allKids.First(k => k.name == name);
Answer by anthoxico · Nov 15, 2018 at 05:55 PM
List<GameObject> childrens = new List<GameObject>();
int count = 0;
while (count < gameObject.transform.childCount)
{
childrens.Add(gameObject.transform.GetChild(count).gameObject);
count++;
}
Your answer
Follow this Question
Related Questions
Creating new Transform from existing objects Transform to Instantiate object 1 Answer
How can I get a parent GameObject of gameObject using Javascript? 6 Answers
Access a child from the parent or other gameObject. 2 Answers
How can I access a child object? 1 Answer
Instantiate Terrain Object as child of Empty Game Object 1 Answer