Why does this not work (door click)

if ( Input.GetMouseButtonDown(0) ){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);

if (Physics.Raycast (ray, hit, 2.0)){

if(hit.collider.gameObject.name == “Door(Clone)”){

var script = hit.collider.gameObject.GetComponent(“Door”);

script.activate();

}
}
}:face_with_spiral_eyes:

Going to have to be more descriptive about whats not working.

It may be a JS thing but in C# you would have to cast what your setting to script with (Door) infront of it.

its in javascript and it says NullReferanceException every time i click.
I connected this to a fps

well looking at the code the places where you may get a null value that you are not checking for are
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var script = hit.collider.gameObject**.GetComponent(“Door”)**;

Camera.main might be null.
If theres no Door component on the hit object that would be null.

before you set ray do this
if( Camera.main ) {
// rest of function
}

and then change your line calling activate() to

if( script ) { script.activate();}

it wasnt the MAIN camera, thank you :slight_smile: