Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

The out keyword in unity not supported

Discussion in 'Scripting' started by NonEssereCavolli, Feb 1, 2018.

  1. NonEssereCavolli

    NonEssereCavolli

    Joined:
    Feb 15, 2015
    Posts:
    26
    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?
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Out works fine. Where do you see that error?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. class Example : MonoBehaviour
    5. {
    6.     void Maths(int a, int b, out int i)
    7.     {
    8.         i = a+b;
    9.     }
    10.  
    11.     void Start()
    12.     {
    13.         int value;
    14.         Maths(8, 4, out value);
    15.  
    16.         Debug.Log(value);
    17.     }
    18. }
     
  3. NonEssereCavolli

    NonEssereCavolli

    Joined:
    Feb 15, 2015
    Posts:
    26
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. namespace Datatypes
    7. {
    8.     public class CardManager : MonoBehaviour
    9.     {
    10.         ...
    11.                     if (Physics.Raycast(new Ray(Input.mousePosition, Vector3.forward), out RaycastHit hitInfo, 10)) //HERE
    12.                     {
    13.                         if (hitInfo.collider.tag == "Hit")
    14.                         {
    15.                             Cm.DealaCard(true, Player.User);
    16.                             return true;
    17.                         }
    18.                         else if (hitInfo.collider.tag == "Stay")
    19.                         {
    20.  
    21.                             return false;
    22.                         }
    23.                 ...
    24.  
    25.     }
    26. }
    27.  
     
  4. NonEssereCavolli

    NonEssereCavolli

    Joined:
    Feb 15, 2015
    Posts:
    26
    Your code is fine with no errors. My code is also in its own namespace if that will do anything to it?
     
  5. NonEssereCavolli

    NonEssereCavolli

    Joined:
    Feb 15, 2015
    Posts:
    26
    SOLVED. I cant declare and do out at same time for some reason
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Move the declaration outside of the method call. That's what the error is about.
    Code (csharp):
    1. RaycastHit hit;
    2. // your call
    3. ... (blah , blah , out hit); // :)
    4.  
     
  7. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    "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:
    Code (C# 7.0):
    1. Physics.Raycast(ray, out RaycastHit hitInfo, 10);
    into this:
    Code (C# 6):
    1. RaycastHit hitInfo;
    2. 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.

    upload_2018-2-1_15-44-50.png

    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.
     
    Last edited: Feb 1, 2018
    Bunny83 likes this.
  8. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,924
    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."
     
  9. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,543
    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.