- Home /
access vector variable from script
Hello, i have a script (misc) attached to a parent game object and i am trying to pass a vector position to a child object script(dragPrey) through script, with the current code i use, i get a null reference exception from the dragPrey script.
var dp: dragPrey;// the script i am trying to access
function Start()
{
dp =transform.FindChild("preyDragger").GetComponent(dragPrey);
}
i would like to know if there is anything i am doing wrong with the above code, thanks
Answer by robertbu · Feb 26, 2014 at 01:25 AM
The Transform class does not have a FindChild() function. If you are talking about 'Find', it will only find first level children. For children deeper than first level, you need to provide a path. See the Transform.Find Reference page for an example. And when Find() finds 'preyDragger', it must have a 'dragPrey' component. If you need to search out the child recursively, Google "Transform find child recursively" for several answers with code.
thank you for clarifying it for me, on the internet you see a lot of wrong explanations, once again thank you
Your answer
Follow this Question
Related Questions
how to access scripts in children 2 Answers
Last time I'll ask for help on this lol 2 Answers
Trouble accessing child 3 Answers