Hello there.
I’m trying to make an interaction script.Everytime the ray hits the object with a certain tag it will run the object’s script named DoorInter.But everytime I try to ray an object, even if it’s not that i get the error
NullReferenceException
CameraRayRecongition.Update () (at
Assets/[Assets &
Prefabs]Scripts/Player/CameraRayRecongition.js:16)
This is the Code :
var length : float = 100.0;
// Update is called once per frame
function Update () {
var drawer : GameObject = GameObject.FindWithTag("Drawer");
var door : GameObject = gameObject.FindWithTag("Door");
var switches:GameObject = GameObject.FindWithTag("Switches");
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
var crosshairScript : CrossHair = GameObject.FindWithTag("MainCamera").GetComponent(CrossHair);
Debug.DrawRay (ray.origin, ray.direction * length, Color.blue);
if(Physics.Raycast (ray, hit,length)){
var coll = hit.collider;
if(coll == drawer.collider){
//var inter : DoorInter = coll.GameObject.GetComponent(DoorInter);
crosshairScript.crosshairChangingDrawer();
// inter.Dosomething();
}
if(coll == door.collider){
// var inter2: DoorInter = coll.GameObject.GetComponent(DoorInter);
crosshairScript.crosshairChangingDoor();
// inter2.Dosomething();
}
if(coll == switches.collider){
crosshairScript.crosshairChangingDoor();
/// var inter3 : DoorInter = coll.GameObject.GetComponent(DoorInter);
// inter3.Dosomething();
}
}
}
Any ideas what should i do?