- Home /
JS, get child
I have an empty which has objects inside and I want to know how to get a child of the empty via name, and the script is inside the empty.
Comment
Best Answer
Answer by aldonaletto · Dec 31, 2012 at 07:10 PM
You can find a child transform by name with transform.Find:
var weapon: Transform = transform.Find("Weapon");
Answer by GlennHeckman_old · Dec 31, 2012 at 09:52 PM
var parent:GameObject = <YourParentGameObjectHere>;
for(var child:Transform in parent.transform)
{
if(child.name == "XYZ")
{
print("You Found ME!");
var myScript:SCRIPNAME = child.GetComponent("SCRIPTNAME");
}
}
whenever i try to use transform.Find i get an error that says "NullReferenceException: Object reference not set to an instance of an object". before you tell me to go read the NullReferenceException section, i have. I am using the correct child name and i dont know WHY the object reference is not set to an instance of an object. Any help would be appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090204im_/https://answers.unity.com/themes/thub/images/avi.jpg)