Raycast Works Different in Unity 4

The following code works differently in Unity 3.x and 4.x:

function Update () {
	
	var hit: RaycastHit;
	var mousePos = Input.mousePosition;
	var theRay = camera.main.ScreenPointToRay(mousePos);

	if (!Physics.Raycast(theRay, hit, 10))
		return;

	if (hit.collider == null) 
		return;		
	
	print (hit.collider);
}

In Unity 3.5.7, I only see output when the mouse is over a collider in the scene. It prints the name of the object the collider is attached to. Otherwise, it never gets to the print line.

In Unity 4.1.2, I see output continuously, even when the mouse is outside the game window. When the mouse is not over a collider in the scene or outside the game window, it prints “Null” (not “null”). If the mouse is over a collider in the scene, it prints the name of the object the collider is attached to, as it does in 3.x. The Raycast function always returns true.

Is there any way to get this to work like it used to, so it only executes when a collider is hit?

your script works like exceptet on my end ( 4.1.2 ), it only prints if the mouse is over a collider. I just tested it.

I tried your code in Unity 3 and Unity 4 and there is no difference. You have some other script running or something.

–Eric

You’re correct. My mistake. I couldn’t figure out how to delete the thread.