CS1503 Argument 3: cannot convert from ‘UnityEngine.RaycastHit2D’ to ‘float’
CS0165 Use of unassigned local variable ‘Hit’
CS1615 Argument 3 may not be passed with the ‘out’ keyword
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class S : MonoBehaviour
{
private MeshRenderer TargetEnemy;
private void Update()
{
RaycastHit hit;
Physics2D.Raycast(transform.position, transform.right, out hit, 5);
}
private void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawRay(transform.position, transform.right * 5);
}
}
There are no “out” parameters in Physics2D.Raycast. It seems like you may be confusing it with the 3D version. Please review the correct usage of the function in the documentation:
1 Like
How To Use This Code In 2D ?
I think the scripting page for this in the docs is pretty self explanatory.
https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
As PraetorBlue has stated above, there is no parameter for “out”.
Rather than passing the value in the method with out. It returns a RaycastHit2D. So if you want to capture that and use it you’ll need to assign the return value to a variable.
Just take your hit variable add 2D to the end of it and assign the return value from the raycast call to the variable.
RaycastHit2D hit = Physics2D.Raycast( arguments );
The first error you are getting is due to passing the raycast where the method is expecting a float argument.
The second is due to passing an unassigned variable as an argument. It’s unassigned and out is not a parameter here, thus you are passing an argument that is unassigned.
The third is due to the fact that Unity has probably seen this happen a lot between mixing parameters and calls for 2d & 3d APIs so they added custom exceptions to be thrown to help people debug for mixing up 2d and 3d APIs.
Hope that helps.