Hi Iam new to unity and i am trying to make script that print name of the closest object to mouse cursor with tag “joint”.It is based on example at this page: Unity - Scripting API: GameObject.FindGameObjectsWithTag but i have an error
NullReferenceException: Object reference not set to an instance of an object
mouse input.Update () (at Assets/mouse input.js:12)
and i dont know what am i doing wrong… Here is the code:
#pragma strict
var mouseDistance : float = 5;
public var distance = Mathf.Infinity;
function Update () {
var mousePosition = new Vector3 (Input.mousePosition.x,Input.mousePosition.y,mouseDistance);
var worldPoint = Camera.main.ScreenToWorldPoint(mousePosition);
if (Input.GetKey(KeyCode.Mouse0))
{
Debug.Log(FindNearestJoint(worldPoint).name);
}
}
function FindNearestJoint (curPos : Vector3)
{
var joints : GameObject [];
var closest : GameObject;
joints = GameObject.FindGameObjectsWithTag("Joint");
for (var object : GameObject in joints)
{
var diff = (curPos - object.transform.position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = object;
distance = curDistance;
}
}
return closest;
}
Iam sorry for my bad english.