Raycast isn't working

I’m trying to make a raycast in c# but I get the following errors:

  • Error CS1061: ‘UnityEngine.Camera’ does not contain a definition for ‘ScreenPointToRay2D’ and no extension method ‘ScreenPointToRay2D’ accepting a first argument of type ‘UnityEngine.Camera’ could be found (are you missing a using directive or an assembly reference?)

  • Error CS1502: The best overloaded method match for ‘UnityEngine.Physics2D.Raycast(UnityEngine.Vector2, UnityEngine.Vector2)’ has some invalid arguments

  • Error CS1503: Argument ‘1’: cannot convert from ‘UnityEngine.Ray2D’ to ‘UnityEngine.Vector2’ (CS1503)

  • Error CS1503: Argument ‘2’: cannot convert from ‘out UnityEngine.RaycastHit2D’ to ‘UnityEngine.Vector2’ (CS1503)

For reference, here’s the code:

protected virtual void OnMouseOver() {
		if (Input.GetMouseButtonDown(0) && !playerControlled) {
			RaycastHit2D hit;
			Ray2D ray = Camera.main.ScreenPointToRay2D(Input.mousePosition);
			if (Physics2D.Raycast (ray, out hit)) {
				// Select this unit
				unitController.SelectUnit(this);
			}
		}
	}

You realise that your script is in MouseOver. Unity has already done the raycasting to call this function. So this script will do your job.

 protected virtual void OnMouseOver() {
     unitController.SelectUnit(this);
 }

For a typical game OnMouseUp would be better. If you are using the latest version of Unity (4.6 or above) then implementing IClickHandler would do even better.