tags:

views:

319

answers:

1

Hi, i'm trying to use the Physics.Raycast method, but I get errors saying:The best overloaded method match for 'UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, float, int)' has some invalid arguments.

It's strange because both itellisense and the documentation tell me that this is permitted:

RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {
    print(hit.point.ToString());
    selection.transform.Translate(hit.point - selection.transform.position);
}

Any idea?

+2  A: 

I think you need the out keyword before "hit" in Physics.Raycast(ray, hit).

RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)) {
    print(hit.point.ToString());
    selection.transform.Translate(hit.point - selection.transform.position);
}
Rod Hyde
Thanks a lot. I never heard of the out keyword before.
sharvey