Problem with Camera.mainCamera.ScreenPointToRay(Input.mousePosition);

I want to get the point, where my mouse is pointing in wold space. But i get this error:

NullReferenceException
UnityEngine.Camera.ScreenPointToRay (Vector3 position)

this is my script:

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

	if(Physics.Raycast(ray, hit, Mathf.Infinity))
	{
           Debug.Log(hit.point);		
	}			
}

The Console says the error is in this line:

var ray : Ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);

… i dont know what is false?! Can someone help me?

Set "mainCamera" to "main": Ray = Camera.main.ScreenPointToRay(Input.mousePosition);

1 Answer

1

I’m guessing that none of your cameras is tagged as “MainCamera”[9339-screen+shot+2013-03-26+at+10.22.41.png|9339]