I’m brand new to Unity and have been going through a Unity textbook to introduce myself to the platform. I’m going through a chapter on raycasting right now and am trying to get this code for the main camera to work:
However, Visual Studio is telling me that I cannot pass the third argument with the ‘out’ keyword. I was going through the raycast documentation on Unity’s website and a few of the officially documented calls for Physics.Raycast use the same arguments as I am.
I don’t understand what’s going wrong. Thanks in advance for the help!
hmm… I just tried the exact same thing and it works for me. What’s the exact error message you’re getting? Also, does Unity have the same error, or just Visual Studio?
Raycast is a HORRIBLY overloaded function. There is something like 17 different variations of it, and unfortunately things like the layermask and distance can be mistaken for each other.
Always name your arguments and this problem will go away.
This is a good thing to check as well. If you’re using Visual Studio then you can hover over the call and hold Ctrl or Cmd (on mac) to see which overload you’re using and make sure the parameters look correct (3rd parameter is out RaycastHit):
+15 overloads. Scary!
That being said I think based on your code you are using the correct one…
I would NOT use them normally all the time, it makes the code look like ObjectionableC code, but for certain poorly-designed functions like Physics.Raycast, I find it to be extremely useful and time saving.
For some reason even when I put the named arguments it said “Cannot convert type RaycastHit to UnityEngine.RaycastHit” so I had to go further and adjust the type to UnityEngine.RaycastHit. And then for some reason for each of the references to the hit object in the print statements GetComponent() became invalid so I had to change it to hit.collider for some reason.
OMG haha. I figured out what the issue was. My file and class were NAMED RaycastHit because that’s what my textbook told me to do. So of course it caused these issues. That’s an oversight by the author haha.
Edit: So looking back in the textbook the author wrote RaycastScript for the file and not RaycastHit. I’m just blind. Thank you all for the help anyways! I learned a lot.
Yep - this actually is the cause of the compiler picking the wrong overload too. Basically it fits none of them therefore the compiler just guesses one.