- Home /
Access children using Find() returns 'null'?
I'm trying to access the children in a prefab. Internet says that I should use: var child = transform.Find("child");
I got it to work once (that file is long gone), but since then it always returns a null. My current scene includes the following hierarchy: vehRigA -axleFront -axleRear
But the following code,
var axleF = transform.Find("axleFront");
function update(){
print(axleF); // returns: null
}
shows that the Find() is not finding.
What am I doing wrong? Should I be using GameObject.Find(),(tried it and got an error) or another kind of Find?
(why can't we access a child by "parent.child"? I thought that was the definition of OO programing?)
parent.child, I guess you mean parent.Find('someName'), which would be finding a sibling of the object, not a child. And it does work in case you have a parent, and its failure to perform what you want has nothing to do with OO program$$anonymous$$g. It's dependent on your hierarchy setup. Does the object your script sits on have a parent node? If it doesn't it'll throw a null reference exception since you'd be calling Find on a null object. (Parent return null if it doesn't have a parent. It doesn't have a parent if, well, it isn't parented to one)
I've had this stuck in my head since reading your variable name: http://www.youtube.com/watch?v=IG8EdbrSVtc
Answer by efge · Mar 20, 2011 at 12:18 PM
Is this script attached to the object "vehRigA"? If not you should use the whole path to the child
(vehRigA may not have a parent in the hierarchy view):
var axleF : Transform;
function Start () { axleF = transform.Find("/vehRigA/axleFront"); //use the full path from root }
function Update(){ ... }
(or use GameObject.Find() which finds every object and child without the path)
Yes, the script is attached to vehRigA. I went ahead and tried anyway (copied from unity scene): var axleF:Transform; function start(){ axleF = transform.Find("vehRigA/axleFront"); } function Update () { print(axleF); //null! } I adapted my code to be like yours to eli$$anonymous$$ate any other errors; previously, I wasn't running the Find() in the start() function. Sadly, it still isn't working.
From the reference: "For performance reasons it is recommended to not use this function every frame Ins$$anonymous$$d cache the result in a member variable at startup."
Answer by invadererik · May 20, 2013 at 04:59 AM
note capitalization: Start vs start (its even colored purple in one versus the other )
Answer by lakehaze · Mar 22, 2011 at 03:07 AM
Okay, seems I'm learning a scripting syntax lesson: Spaces are important; you must put spaces everywhere or it does not work.
//does not work var axleF:Transform;
function start(){ axleF = transform.Find("/vehRigA/axleFront"); } function Update () { print(axleF); }
//works var axleF : Transform;
function Start () { axleF = transform.Find("/vehRigA/axleFront"); }
function Update(){ print(axleF); }
Thanks for your help :)
Thank you for validating the bizarre-ness of this. Over time, this post has festered in my brain. I come back now and again, and just stare at the issue. Seems like the devs might be curious about what's causing this.