- Home /
Unity asking me /not/ to add out to a raycast
I am really very confused, here. Unity is politely asking me not to add the out modifier to a raycast. Any ideas why? I searched the web for people with similar problems, but alas.
The error is here:
error CS1615: Argument #3' does not require
out' modifier. Consider removing `out' modifier
And the code is here:
RaycastHit2D hit;
if (Physics2D.Raycast(Player.position, Vector2.up, out hit))
{
updistance = hit.distance;
}
And, just in case somebody wonders, Player is a transform variable.
Answer by rutter · Sep 17, 2014 at 09:00 PM
Unity's 3D raycast has traditionally used two return values: a boolean indicating whether or not you hit anything, and an optional out
parameter to populate a RaycastHit.
The 2D raycast handles things differently, with a single return value: you get a RaycastHit2D back, and it'll just be null if you didn't hit anything.
RaycastHit2D hit = Physics2D.Raycast(Player.position, Vector2.up);
if (hit != null) {
//you hit something
}
You might be used to one or the other, which can be confusing when switching.
If you'll be raycasting a lot, you can avoid extra GC allocation by using Physics2D.RaycastNonAlloc instead.
So, are you saying that there is no way to collect hit data from a Vector2 raycast?
I apologize, I just read it the wrong way. Thank you, rutter, and thank you, tanoshimi. This did help.
Your answer
Follow this Question
Related Questions
Raycasting error 1 Answer
How to use Raycast to open a crate? 2 Answers
Question about GetComponent 1 Answer
need help converting gun script from c# to javascript 1 Answer
Test line renderer collision? 1 Answer