The out keyword in unity not supported

Hi,
I am trying to create a raycast with an out parameter but it won’t let me, saying that:
Feature `declaration expression’ cannot be used because it is not part of the C# 6.0 language specification

What can I do or are there any work arounds?

Out works fine. Where do you see that error?

using UnityEngine;

class Example : MonoBehaviour
{
    void Maths(int a, int b, out int i)
    {
        i = a+b;
    }

    void Start()
    {
        int value;
        Maths(8, 4, out value);

        Debug.Log(value);
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Datatypes
{
    public class CardManager : MonoBehaviour
    {
        ...
                    if (Physics.Raycast(new Ray(Input.mousePosition, Vector3.forward), out RaycastHit hitInfo, 10)) //HERE
                    {
                        if (hitInfo.collider.tag == "Hit")
                        {
                            Cm.DealaCard(true, Player.User);
                            return true;
                        }
                        else if (hitInfo.collider.tag == "Stay")
                        {

                            return false;
                        }
                ...

    }
}

Your code is fine with no errors. My code is also in its own namespace if that will do anything to it?

SOLVED. I cant declare and do out at same time for some reason

Move the declaration outside of the method call. That’s what the error is about.

RaycastHit hit;
// your call
... (blah , blah , out hit); // :)

“Declaration Expression” is the name of a feature originally targeted for C# 6 but eventually bumped to C# 7.0.
As you already discovered, variables need to be declared before they can be used as out variables when using C# 6. The feature is basically syntactical sugar (aka: short hand) to convert this:

Physics.Raycast(ray, out RaycastHit hitInfo, 10);

into this:

RaycastHit hitInfo;
Physics.Raycast(ray, out hitInfo, 10);

You’ll notice that in addition to the extra line of code, I don’t include hitInfo’s type in the method call.

I’m guessing you are using Visual Studio 2017, which is why the warning knows about this thing called “Declaration Expression”. Otherwise, I would have expected a different warning like “unexpected use of type RaycastHit when identifier was expected”, or “hitInfo is not declared”…

You might be able to use this language feature if you change your project’s targeted version of C#, which is an “advanced” option in the project’s build properties.

The reason I say might, is because some language features are tied to the framework version, which is not as flexible to change when working with Unity.

EDIT
Looks like C#7 support is a lot more complicated than I thought , so better ignore the bottom half of this post.

1 Like

It’s not for “some reason” - it’s the way variables always work. You don’t include the type when you use one, only when you declare it. The function heading is declaring the inputs, so it counts. You don’t write x = 2 + int y; or if(int x<3) or Mathf.Sqrt(float q);. You’ve already told the computer what type those are.

It’s a common mistake. I make a point of showing it in each of those cases. It’s easy to forget int x=7; is really a 2-statement shortcut. And it’s very common for students to add the type in a function call like you did (essentially confusing parameters and arguments.)

Even that wrong error (declaration expression) is common. You mess up, the compiler thinks you were attempting some crazy feature no one has ever used and gives you a bizarre error message. Just remember sometimes an error is simply “something is wrong with that line.”

Please start your own thread. Your issue has nothing to do with the original question in this thread. You should take a look at the documentation. A raycast needs a starting position and a direction. You only passed a direction. Also the Raycast method has several overloads with different arguments. They have to be in the right order.

Finally when you post code here on the forum, please use code tags. There’s an “insert code” button in the toolbar.