Cannot use out command with RaycastHit object when calling Physics.Raycast

Hi!

I’m brand new to Unity and have been going through a Unity textbook to introduce myself to the platform. I’m going through a chapter on raycasting right now and am trying to get this code for the main camera to work:

    void Update()
    {
        float dirX = Input.GetAxis("Mouse X");
        float dirY = Input.GetAxis("Mouse Y");

        transform.Rotate(dirY, dirY, 0f);
        CheckForRaycastHit();
    }

    void CheckForRaycastHit()
    {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, transform.forward, out hit))
        {
            print(hit.GetComponent<Collider>().gameObject.name + " destroyed!");
            Destroy(hit.GetComponent<Collider>().gameObject);
        }
    }

However, Visual Studio is telling me that I cannot pass the third argument with the ‘out’ keyword. I was going through the raycast documentation on Unity’s website and a few of the officially documented calls for Physics.Raycast use the same arguments as I am.

I don’t understand what’s going wrong. Thanks in advance for the help!

hmm… I just tried the exact same thing and it works for me. What’s the exact error message you’re getting? Also, does Unity have the same error, or just Visual Studio?

This works okay for me

        if(Physics.Raycast(new Vector3(), transform.forward, out hit))
        {
            Debug.Log("hit");
        }

This is the exact error. It is also printed into the Unity debug console.

6251682--689073--Screen Shot 2020-08-27 at 3.52.46 PM.png

This gives me the same error… Super bizarre. Unity seems to have an issue specifically with me calling ‘out’ for the hit object.

Really strange… What version of Unity/Visual Studio are you using?

Raycast is a HORRIBLY overloaded function. There is something like 17 different variations of it, and unfortunately things like the layermask and distance can be mistaken for each other.

Always name your arguments and this problem will go away.

This is a good thing to check as well. If you’re using Visual Studio then you can hover over the call and hold Ctrl or Cmd (on mac) to see which overload you’re using and make sure the parameters look correct (3rd parameter is out RaycastHit):
6251757--689079--upload_2020-8-27_16-15-58.png

+15 overloads. Scary!
That being said I think based on your code you are using the correct one…

I’m using 2019.4.8f1

What do you have set under Edit → Project Settings → Player → Other Settings → Api Compatibility Level ?

Yep! That’s what’s happening. It’s confusing the different overloads for parameters i’m using. How can I force it to use a certain overload?

6251826--689091--Screen Shot 2020-08-27 at 4.25.52 PM.png

.NET Standard 2.0
I’m on mac btw.

As I wrote above, named arguments work 100% of the time.

So what does this mean? Like you declare the variable/object type for each argument?

LMGTFY: Named and Optional Arguments - C# | Microsoft Learn

I would NOT use them normally all the time, it makes the code look like ObjectionableC code, but for certain poorly-designed functions like Physics.Raycast, I find it to be extremely useful and time saving.

That fixed it! Partially…

void Update()
    {
        float dirX = Input.GetAxis("Mouse X");
        float dirY = Input.GetAxis("Mouse Y");

        transform.Rotate(dirY, dirX, 0f);
        CheckForRaycastHit();
    }

    void CheckForRaycastHit()
    {
        UnityEngine.RaycastHit hit;
        if (Physics.Raycast(origin: transform.position, direction: transform.forward, hitInfo: out hit))
        {
            print(hit.collider.gameObject.name + " destroyed!");
            Destroy(hit.collider.gameObject);
        }
    }

For some reason even when I put the named arguments it said “Cannot convert type RaycastHit to UnityEngine.RaycastHit” so I had to go further and adjust the type to UnityEngine.RaycastHit. And then for some reason for each of the references to the hit object in the print statements GetComponent() became invalid so I had to change it to hit.collider for some reason.

But it works finally!

You must have some OTHER kind of RaycastHit in your project.

If you care to find out, remove UnityEngine from that line and hover over it, see the full namespace.

OMG haha. I figured out what the issue was. My file and class were NAMED RaycastHit because that’s what my textbook told me to do. So of course it caused these issues. That’s an oversight by the author haha.

Edit: So looking back in the textbook the author wrote RaycastScript for the file and not RaycastHit. I’m just blind. Thank you all for the help anyways! I learned a lot.

Yep - this actually is the cause of the compiler picking the wrong overload too. Basically it fits none of them therefore the compiler just guesses one.

I’ve been suffering from the exact same issue all day. Thank you so much!