# Physics.Raycast won't use right method

**URL:** https://discussions.unity.com/t/physics-raycast-wont-use-right-method/115227
**Category:** Questions & Answers
**Created:** [August 29, 2014, 2:55am UTC](https://discussions.unity.com/t/physics-raycast-wont-use-right-method/115227 "2014-08-29T02:55:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![TheIncognito](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@TheIncognito](https://discussions.unity.com/u/TheIncognito)
#### Post date: [August 29, 2014, 2:55am UTC](https://discussions.unity.com/t/physics-raycast-wont-use-right-method/115227/1 "2014-08-29T02:55:06Z")

</div>

if (Input.GetButton (“Fire1”)) {  
if(Physics.Raycast(ray, transform.forward, hit, 300)) {

```
			}
		}

```

When I have named all these variables (ray and hit), it tells me that I’m using Physics.raycast wrong.

ERROR MESSAGE: Assets/Custom Assets/Scripts/ShootGun.cs(19,36): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Ray, out UnityEngine.RaycastHit, float, int)’ has some invalid arguments

---

<div class="post-metadata">

### Author: ![kacyesp](https://avatars.discourse-cdn.com/v4/letter/k/6f9a4e/32.png) [@kacyesp](https://discussions.unity.com/u/kacyesp)
#### Post date: [August 29, 2014, 3:04am UTC](https://discussions.unity.com/t/physics-raycast-wont-use-right-method/115227/2 "2014-08-29T03:04:51Z")

</div>

I think you may have just messed up the types.  
I would double check what types you declared “ray” and “hit” as and make sure you match one of the function overloads specified in the docs: [Unity - Scripting API: Physics.Raycast](http://docs.unity3d.com/ScriptReference/Physics.Raycast.html)

---

<div class="post-metadata">

### Author: ![AyAMrau](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@AyAMrau](https://discussions.unity.com/u/AyAMrau)
#### Post date: [August 29, 2014, 3:08am UTC](https://discussions.unity.com/t/physics-raycast-wont-use-right-method/115227/3 "2014-08-29T03:08:48Z")

</div>

You need to put the out keyword before the RaycastHit parameter:  
Physics.Raycast(ray, transform.forward, out hit, 300)

You also may be mixing the version that takes origin and direction as vectors with one that just takes a ray. They’re multiple versions so check the documentation

---

<div class="post-metadata">

### Author: ![TheIncognito](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@TheIncognito](https://discussions.unity.com/u/TheIncognito)
#### Post date: [August 29, 2014, 11:00am UTC](https://discussions.unity.com/t/physics-raycast-wont-use-right-method/115227/4 "2014-08-29T11:00:42Z")

</div>

Found it:

Turns out that Camera.main.ScreenPointToRay(Screen.width / 2, Screen.height / 2, 0) makes transform.forward create panic in unity 😕
