- Home /
help! I can't get an array from another script
at the end of the start function there is a function that says:
var tunnels : tunnels = gameObject.GetComponent(tunnels);//name of script
var nodes : Vector3[] = tunnels.nodes;//name of array
it returns... null reference exception-not set to instance of an object
and I can see in the editor prior to and during play, it clearly is an array with values. I just can't seem to get it.
I attempted to wait yield 10 seconds before even calling the lines to get the array, but it says null reference exception straightaway as it loads.
this also returned a null reference exception:
if ( ntunnels.nodes[2] == null ){var test = Vector3.one;}
what am I doing wrong?
just to be clear, you $$anonymous$$UST CHANGE your variable name to something different. what about happyTunnels. do not use "tunnels".
Answer by robertbu · Mar 15, 2013 at 07:01 PM
Your variable name is the same name as your type. Assuming your script name was "tunnels" and assuming that the script was attached to the same game object as the code above you can do:
var theTunnels : tunnels = gameObject.GetComponent(tunnels);
var notes = theTunnels.notes;
thank you, it didn't solve it, it still says there is a null reference exception. do you know why the error appears straight away even when there is a WaitForSeconds (8) at the top of the function?
WaitForSeconds should only be used inside a coroutine. So its not working as you would expect in the Start() function
you want something like this:
function WaitBeforeAssign () {
// suspend execution for 8 seconds
yield WaitForSeconds (8);
var theTunnels : tunnels = gameObject.GetComponent(tunnels);
var notes = theTunnels.notes;
}
then call this from your Start() in place of the 2 assignment calls:
StartCoroutine(WaitBeforeAssign());
try that and see if you still get the null reference on start
Are you absolutely sure that is a tunnels script attached to the same game object as this script? Not having the script attached to the same game object will cause this error. If it is attached to a different game object, then you need to find that object first and then call GetComponent on that object.
@brianruggieri - Start functions may have a yield statement in Javascript, because they are allowed to be coroutines automatically without starting them explicitly. In C# you have to declare them as IEnumerator Start
Why don't you post both scripts here or open a new question with both scripts concerning why you are having trouble with your Vector3. From the little bit here, I cannot figure out why you are having trouble.
Your answer
Follow this Question
Related Questions
Smooth Mouse Script Problems 0 Answers
Raycast Having Pointless Errors 1 Answer
Array not working as expected 0 Answers
Unity opening fails 0 Answers