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.
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.