Compile Error cs1003 on fps, pls help

I was just fishishing up a gun script and got this error: cs1003. I spent about an hour trying to figure out why. Can anyone please help. The exact error is (24, 52)cs1003. Here is code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GunScript : MonoBehaviour
{
private Camera PlayerCamera;

public GameObject robot;

bool shoot = false;

void Start()
{
    PlayerCamera = Camera.main;
}

void Update()
{
    if (Input.GetMouseButtonDown("0"))
    {
        Ray gunray = new Ray(PlayerCamera.transform.position, PlayerCamera.transform.forward);

        if (Physics.Raycast(gunray, RaycastHit hitInfo, robot))
        {
            Destroy(robot);
        }
    }

}

}

Two things. You don’t need the quotes around the button number in the GetMouseButton method. Just use if (Input.GetMouseButton(0))

You have confused the parameters in the Raycast. There are 4 different versions of this command, depending on which parameters you pass. Firstly, you need the word “out” before the RayCastHit. That ensures that the Raycast will populate your hitInfo struct with the appropriate data. However, I don’t think there is a version of Raycast that accepts a Ray, a RaycastHit and a gameObject (Robot).

Have a look at the fourth declaration section on this page to see that the third parameter needs to be a distance. Perhaps you only need the Ray and the RaycastHit, in which case the normal methods of checking data coming back from a RaycastHit are:

  • if(hitInfo.transform.gameObject.CompareTag("Robot")) (but you’ll need to give the Robot a tag of “Robot” first
  • if(hitInfo.transform.name == "Robot")

Another option (and often a better one) is to put the Robot on a layer by itself and then pass the layer name to the Raycast. Then you only need to test for the Ray having hit something and not test what the hitInfo contains.

One last thing. It’s better to define hitInfo on a line by itself, rather than nest it in the call. Even though I know C# pretty well, I momentarily had to ask myself about the scope of something declared in a parameter list. Keeping your code simple and avoid anything that might cause you to question it later.