- Home /
Object reference not set to an instance of an object.
I am making a 1 scene menu with teleporting but I always get the message: NullReferenceException: Object reference not set to an instance of an object. This is the code:
var dest : Transform;
var clicksound : AudioClip;
var Player : Transform;
function Update()
{
if (Input.GetMouseButtonDown(0) ){
var hit : RaycastHit;
if ( hit.collider.gameObject.tag == "Link" ){
AudioSource.PlayClipAtPoint(clicksound, dest.position);
Player.position = dest.position;
}
}
}
Answer by robertbu · Feb 26, 2013 at 07:36 PM
You don't say what line the error is on, but you are missing a Raycast(). You are using an uninitialized hit variable on line 9. I'm not sure where you want the ray to point.
Here is an example casting from the center of the screen (untested):
var dest : Transform;
var clicksound : AudioClip;
var Player : Transform;
function Update()
{
if (Input.GetMouseButtonDown(0) ){
var ray : Ray = Camera.main.ViewportPointToRay(Vector3(0.5, 0.5, 0));
var hit : RaycastHit;
if (Physics.Raycast(ray, hit))
{
if ( hit.collider.gameObject.tag == "Link" ){
AudioSource.PlayClipAtPoint(clicksound, dest.position);
Player.position = dest.position;
}
}
}
}
It didn't work but checked it and error is at line 8.
I just tried it, and it worked just fine. No errors at either compile time or run time. Note dest, and Player need to be assigned. Select the game object this script is attached to, then drag and drop those resources on top of the variables.
Your answer
Follow this Question
Related Questions
Game not working correctly in build mode? 0 Answers
IsFinite(outDistanceForShort) error message 1 Answer
GUI Error: You are pushing more GUIClips than you are popping. Make sure they are balanced) 0 Answers
Is SetActive doesn't working or the button dont work? [Solved] 1 Answer
completely lost noob 1 Answer