SOLVED NullReferenceException : Object Reference HELP!

hi, i have a very basic scripting knowledge and its my first assignment at universtiy using this program…

Im trying to make a an object play audio as a user clicks on it, and then loop the pattern of mouse clicks, so user can create original music.

After i fix this error i will be creating a loop funciton witch will play audio based on the timesdiff array.

The error is on this line.

timesdiff[counter-1] = times[counter] - times[counter-1];

This is my whole script.

var times = new Array( 8 );
var counter : int = 0;
var timesdiff = new Array( 8 );
var boom : AudioClip;

function OnMouseUp () {
renderer.material.color = Color.red;
times[counter] = Time.realtimeSinceStartup;
print(times);
print(counter);
counter ++;
if(counter != 0) {
diffCalc ();
}
}

function diffCalc () {

timesdiff[counter-1] = times[counter] - times[counter-1];
print(timesdiff);
}

My error message.

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs)
AudioScript.diffCalc () (at Assets\Scripts\AudioScript.js:20)
AudioScript.OnMouseUp () (at Assets\Scripts\AudioScript.js:13)
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
UnityEngine.MouseOverPair:SendMessage(String)
UnityEngine.SendMouseEvents:smile:oSendMouseEvents()

THANKS

I attached this script to a cube in unity, as well as a prefab i made from a model imported from MAYA, both have a box collider attached to it and the script works fine till the timesdiff array, times array and counter print to console perfectly.

timesdiff[counter-1] = times[counter] - times[counter-1];

times[counter] = Time.realtimeSinceStartup;
print(times);
print(counter);
counter ++;
if(counter != 0) {
diffCalc ();

The problem is that, at the first clip, well, ok, you defined times[counter]. But times[counter - 1] has not been defined, yet you try to do operations with it (substraction). So, Unity go bananas :wink:

Edit : to be more precise, it is rather times[counter] that is not defined, since when you increment counter, it is in fact times [counter -1] that has been affected.

Im really new to scripting and unity, started bascially 2 weeks ago…

how would i go about defining times[counter]
and accessing the values in the array using my counter integer variable. i can get it to work just using times[1] and times[0] and subtracting those.

sorry im really new to this so im not 100% sure on what u mean

What I mean is, when you click, you assign a value and you try to substract that value by the previous one.

But when you click for the first time, you only have one value. What is the previous value ? None, you don’t have it yet. times[0] contains an int, but times [1] is empty. And empty do not have a type, it is not an int, and Unity won’t (shouldn’t) use 0 by default.

but above i have an if function so that the diffCalc() function that subtracts only runs when counter != 0 so when its 1 or higher then the user has clicked at least twice, so there are two values?

im hoping im nt just saying something stupid.
Im assuming fixing it is just a line of code somewhere? coudl u write waht that is i mite understand better if u show me the fix.

When you haven’t clicked yet, there is nothing in your array.

When you click once, you define times [0]. You increase counter, it becomes 1.

Since counter !=0, the function is called, it try to substract two values. You have define times [0], but what about times [counter] ? counter equals 1, but you haven’t defined times [1] yet.

You may want to use the function Add instead. You add elements to your array. When the length of the array is at least 2, you can call the function.

http://unity3d.com/support/documentation/ScriptReference/Array.html

THANKS VERY MUCH,

sorry
the reason i got confused is i had my print(counter); command before counter ++ so i assumed counter was one lower than it was… it works perfeclty now… thanks for being patient with me… lol