MonoDevelop throws System.NullReferenceException while trying to make RayCast work

Hi, I have a bit of an issue…

I’m trying to make a raycast work between a specific camera and my mouse, but it doesn’t seem to work… on debug, MonoDevelop throws System.NullReferenceException “Object reference not set to an instance of an object”…

This is my code thusfar:

private GameObject FPSCam;

void Start ()
{
	FPSCam = GameObject.FindWithTag ("FPSCamera");
}

void Update ()
{
	RaycastHit target;
	Vector3 PositionMouse = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
	Physics.Raycast(FPSCam.transform.position, PositionMouse, out target, 100);
	Debug.DrawRay(FPSCam.transform.position, target.point);
}

And yes, the FPSCamera properly exists. I have another script that toggles between that camera and another called TPSCamera when you right click, and that one works great, using the same FindWithTag function…

Also, the Unity Scripting API gives this example

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
    Debug.DrawLine(ray.origin, hit.point);

to use instead of my attempt, but that gave the same error…

Anyone got any ideas?


EDIT:

Turns out the problem isn’t the Raycast, defining Ray or whatever… but instead, the exception happens because it can’t find the input somehow.

I suspect this is the part that causes trouble:

Input.mousePosition

CLOSING STATEMENT:

Since discovering the problem was Input.mousePosition and not Ray or anything else, I have been looking around, and discovered ScreenPointToRay(Input.mouseposition) throws nullreferenceexeption - Unity Answers

There, they describe that due to some coding error in Unity or whatever reason… Unity doesn’t recognize Input.mousePosition as long as you’re using a camera NOT tagged with ‘MainCamera’.

As both of my Cameras had different tags (namely, FPSCamera and TPSCamera) Unity apparantly had no reference for Input.mousePosition… despite having a script that set the active camera to Camera.Current… but still no Main Camera caused it.

Long story short…

If you use RayCast, make sure the camera you use for it is tagged as MainCamera.

This is the code I use in my game and it works perfectly.

    cam = GameObject.Find("Main Camera").camera;
    Vector3 mousePos = Input.mousePosition;
		if (Input.GetMouseButtonDown(0))
		{
			Ray mouseRay = Camera.main.ScreenPointToRay(mousePos);
			RaycastHit hit;
			if (Physics.Raycast(mouseRay, out hit))
			{
				//manipulate result using RaycastHit hit
			}
		}

From what I can tell, you are trying to pass the Camera GameObject to the ray, instead of the Camera component.

CLOSING STATEMENT:

Since discovering the problem was Input.mousePosition and not Ray or anything else, I have been looking around, and discovered ScreenPointToRay(Input.mouseposition) throws nullreferenceexeption - Unity Answers

There, they describe that due to some coding error in Unity or whatever reason… Unity doesn’t recognize Input.mousePosition as long as you’re using a camera NOT tagged with ‘MainCamera’.

As both of my Cameras had different tags (namely, FPSCamera and TPSCamera) Unity apparantly had no reference for Input.mousePosition… despite having a script that set the active camera to Camera.Current… but still no Main Camera caused it.

Long story short…

If you use RayCast, make sure the camera you use for it is tagged as MainCamera.