Trying to display object name of raycast hit

Hey

All I’m trying to do is get the name of a raycast mouse over. But with this current code, I’m getting Null reference exceptions. Anything I’m doing that’s wrong here?

function Update ()
{
     var hit : RaycastHit;														
     var ray = Camera.main.ScreenPointToRay ( Input.mousePosition );	

     if (Physics.Raycast (ray, 30) && Input.GetMouseButtonDown(0)) 
	{
		print (hit.collider.gameObject.name);
	}
}

That’s because you don’t assign the raycast to the hit var. try this:

function Update ()
{
     var hit : RaycastHit;                                 
     var ray = Camera.main.ScreenPointToRay ( Input.mousePosition );    
     hit = Physics.Raycast (ray, 30);
     if (hit && Input.GetMouseButtonDown(0)) 
     {
       print (hit.collider.gameObject.name);
     }
}