- Home /
Error with Raycast hit.transform
So I am getting an error on the 21st line of this code saying:
"error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, float, int)' has some invalid arguments"
I'm not sure what the issue is. I don't think it has anything to do with the way I set up the raycast. I'm not sure what I'm doing wrong here, and I would appreciate the help.
Here's the code:
private Transform hitObjTrans;
private GameObject global;
private GameObject blockObj;
private RaycastHit hit;
private bool up = false;
void Awake()
{
global = GameObject.Find("Global");
}
void Update ()
{
if (Input.GetMouseButtonUp(0))
{
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
foreach (Transform block in global.GetComponent<GlobalVar>().blocks)
{
if (hit.transform == block)
{
block.GetComponent<BlockVar>().Boom(); //accesses variable that is in all blocks.
}
}
}
}
if (Input.GetKeyDown("e"))
{
if(up)
{
up = false;
blockObj.transform.parent = null;
}
else
{
up = true;
if (Physics.Raycast(transform.position, transform.forward, 1.5f, out hit))
{
blockObj = hit;
hit.transform.parent = transform;
}
}
}
}
Answer by robertbu · Mar 25, 2014 at 07:01 PM
If you take a look at the reference page for Physics.Raycast() you see this for the version of the Raycast() you are trying to setup:
static bool Raycast(Vector3 origin, Vector3 direction, RaycastHit hitInfo, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers);
You have the distance and the RaycastHit parameters reversed in your call.
That actually got rid of a few other errors that I thought were linked with the first, thanks! But is still has the error I was talking about earlier. Now that I look at it, I think the error is actually on line 21. I'm not sure why, though. I'll edit the post to show that.