C# Raycast overloaded function problem

I’m trying to use one of the physics.Raycast() overloads, specifically Raycast( Ray, RaycastHit, Distance, LayerMask)

	void Update () {
	
		if ( Input.GetMouseButtonDown (0) ) {
		
			Ray 		ray;
			RaycastHit	hitInfo = new RaycastHit();
		
			ray = Camera.main.ScreenPointToRay( Input.mousePosition );

	        if ( Physics.Raycast( ray, hitInfo ) ) {
	        	
	        	destinationObject.transform.position = hitInfo.point;	
	        }
		}
	}

But when I compile the compiler can’t find the correct overload. I get the errors:

  • The best overloaded method match for ‘UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3)’ has some invalid arguments
  • Cannot convert from ‘UnityEngine.Ray’ to ‘UnityEngine.Vector3’

Any idea why?

RaycastHit is an “out” parameter. In C# you have to write “out” in the calling site as well, like

Physics.Raycast( ray, out hitInfo )
1 Like

Aah, thank you.