Null reference exception and arrays?

Well, I must be doing something insanely stupid, but I can’t see what. Basically, I’m doing something like this:

private var somearray : Array;

function Start () {
 someArray = new Array();
}

function Awake () {
 someArray.Push(1);  //<- throws exception error.
}

“NullReferenceException: Object reference not set to an instance of an object”

However, this works fine:

private var somearray : Array;

function Start () {
 someArray = new Array();
 someArray.Push(1); 
}

Why? :slight_smile:

Probably because Awake runs before Start. Or because “somearray” is not the same as “someArray”.

–Eric

The case issue was just sloppy example code. The Awake/Start order was the culprit - I did it backwards. Oops. :slight_smile:

Many thanks for the swift help. Going to get some espresso now to get my brain back into gear. :wink:

Cheers,
Dan

if (espresso)
    Awake();
else
    rigidbody.Sleep();

Unity: It’s Not Just For Computers Anymore!™ (Hopefully this code works…I don’t actually drink coffee so I can’t test it, sorry!)

–Eric

Hehe - that about sums it up as far as this (sometimes rigid) body is concerned. :wink:

//Dan