- Home /
Problem With Raycasts, Interaction Script.
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?
Answer by fafase · Dec 29, 2012 at 10:44 AM
First off, "bumping post" is not really the way to go. Some forum would delete you for this kind of prctice (App Hub).
Now you need to understand the difference between GameObject and gameObject as I think this is where you are wrong.
var door : GameObject = gameObject.FindWithTag("Door");
THis shoud not work as FindWithTag is a static function.
var inter : DoorInter = coll.GameObject.GetComponent(DoorInter);
This should be
var inter : DoorInter = coll.gameObject.GetComponent(DoorInter);
Using GameObject, you refer to the actual class. You use this for static methods like FindWithTag.
gameObject is used for instance methods like GetComponent
So when you want to do something that is inherent to the object use gameObject. If you want to do something general like finding an object in the scene, use GameObject.
An outstanding explanation.
"you need to understand the difference between GameObject and gameObject as I think this is where you are wrong"
quite right...
Just to repeat, "gameObject" with a $$anonymous$$iscul "g" refers "to you" - to the thing at hand.
(Technically speaking, to the thingy to which the script is attached.)
On top of that, I now realize all those GetComponent and Find functions are in the Update. This is about to get real slow.
You should consider moving at least the first three lines in the Start so that they only happen once with the variables being global.
I see, and yea sorry for bumping :P Thanks a lot for your information :D