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;
}
}
}
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;
}
}
}
}
I tested it and sadly it didn’t work.
Now it only says NullReferenceException.