Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Resolved Switch expression not working in Unity 2020.3.3f1?

Discussion in 'Scripting' started by SunnyValleyStudio, Jun 30, 2021.

  1. SunnyValleyStudio

    SunnyValleyStudio

    Joined:
    Mar 24, 2017
    Posts:
    67
    Hey!

    I've read that from Unity 2020.2 onwards some c#8 features like switch expressions are available in Unity.

    I have this simple code piece that works for me in Unity 2020.2.1f:
    Code (CSharp):
    1. public State GetState(StateType stateType)
    2.         {
    3.             return stateType switch
    4.             {
    5.                 StateType.Idle => Idle,
    6.                 StateType.Move => Move,
    7.                 StateType.Fall => Fall,
    8.                 StateType.Climbing => Climbing,
    9.                 StateType.Jump => Jump,
    10.                 StateType.Attack => Attack,
    11.                 _ => throw new System.Exception("State not defined " + stateType.ToString()),
    12.             };
    13.         }
    A friend with unity 2020.3.3f1 tried to type it and received an error:

    His Visual Studio IDE doesn't even show a quick help option to convert a default switch statement to switch expression which works perfectly fine in my project:
    upload_2021-6-30_10-56-14.png

    Can there be any specific reason for this behaviour? Can outdated IDE cause the issue? I would assume that it isn't caused by mistakenly creating a project in an older Unity version.

    Thanks!

    PS.
    Project is indeed in 2020.3.3f1 so theoretically switch expressions should work there by default.
     
    Last edited: Jul 1, 2021
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,960
    Did they see the error in Unity or just in their IDE?

    Have them regenerate project files in Unity and make sure their IDE is configured properly to work with Unity.
     
    SunnyValleyStudio likes this.
  3. SunnyValleyStudio

    SunnyValleyStudio

    Joined:
    Mar 24, 2017
    Posts:
    67
    Thanks for an answer. It seems like its a new PC with unity and VS installed together - so I assume that any configuration was done automatically during the installation of unity. I will double check just to be sure.
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,656
    Think it might be the Project version, if you open your Project in a text editor you can change which version of C# you're using.

    upload_2021-7-1_7-11-19.png

    This is my project version, and it doesn't work for me.
     
    SunnyValleyStudio likes this.
  5. SunnyValleyStudio

    SunnyValleyStudio

    Joined:
    Mar 24, 2017
    Posts:
    67
    Thanks a lot for an answer!

    Solution:
    The issue was Visual studio version. My friend wa using VS 2017 and apparently c# 8 is only supported in VS 2019.

    Thank you all for help!
     
    Last edited: Jul 1, 2021
    PraetorBlue likes this.