Null Reference when trying to set a distance in which the cursor changes colour

The script below is attached to any object that I want to interact with (later on) what is meant to happen is that when the camera/FPC is a certain distance away from the object with the cursor on it, the colour should revert back to the normal colour when its not on any object, but I keep getting NullReference and I know it has to do with the variable cam but i’m just not sure what to do.

var gamePointer : GameObject;
private var cam : GameObject;
var triggerDistance : float = 7.0;

function Start() {
cam = GameObject.Find(“Main Camera”);
}

function OnMouseEnter () {
print (DistanceFromCamera());
if (DistanceFromCamera() > triggerDistance) return;
gamePointer.SendMessage(“CursorColorChange”, true);
}

function OnMouseExit(){
gamePointer.SendMessage(“CursorColorChange”, false);
}

function DistanceFromCamera() {
var heading : Vector3 = transform.position - cam.tranform.position;
var distance : float = Vector3.Dot(heading, cam.transform.forward);
return distance;
}

If anyone can help me I’d be really grateful.

Have you about making the cam variable non private. Like that you can directly assign it and be sure that it is not null.

I’ve tried that and i just get the same problem on the following line -

var heading : Vector3 = transform.position - cam.tranform.position;

Even though the camera has been defined.

There is a syntax error. It should be

var heading : Vector3 = transform.position - cam.transform.position;

You forgot the s in transform.

But this does not produce a null reference exception.

OMFG i am actually retarded thank you so much