How to setup Unity 2017 and Visual Studio 2015 to use C# 6. (Step by Step)

Unity 2017 brought support for C# version 6. Unfortunately it doesn’t work out of the box with Visual Studio. Trying to google the problem I wasn’t able to find an all-in-one solution, but step by step I was able to work it out. This should work with a clean new project as well as with a previously created one.

The following is a step-by-step-guid to get things working. For the hasty ones: There is a summary of all steps at the end of this post. For the rest: I tryed my best to explain what we’re doing here. Don’t blame me if If my explanations are not 100% correct. I wouldn’t call myself an advanced user, if I didn’t have to for this to work. 😉

1. Get Unity to use DotNet 4.6

C# 6 is part of DotNet v.4.6. Unity by standart uses DotNet v.3.5. To change this, in Unity go to Edit => Project Settings => Player. Under Configuration change Scripting Runtime Version from Stable (.Net 3.5 Equivalent) to Experimental (.Net 4.6 Equivalent). Unity should now be ready to run C# 6 code.

2. Getting access to your project properties

Create a new C# script or choose an existing one and double click it to start Visual Stuio. (Im using VS 2015 v.3 but I think it should work in other versions too.) If you want to see how it doesn’t work yet, just insert some C# 6 exclusive code to a valid part of your script. For example:

int test {get;}

VS should mark this as erraneous, saying “Feature ‘readonly automatically implemented properties’ is not available in C# 4. Please use language version 6 or greater.” As you may have guessed, we also have to configure VS to use C# 6.

The C# version used is set in VS’ Project Properties. They are located under Project as the bottom menu item. (Instead of project the name of your project is used.) The problem is: If you klick it, the screen flashes and nothing happens. This is because Unity preconfigured the project for you and since you could potentially mess up your whole VS-Unity-pipeline playing with the project settings, the developers made it acessible only if you consider yourself an advanced user. So let’s tell VS that you are.

Navigate to Tools => Options. This should open up a window with a list of cathegories on the left. Choose Tools for Unity. Here under Miscellaneous (on the right side of the window) change Access to project properties to true, wich of course grants you said access to the project properties. Klick Ok to close the window.

3. Change project properties to use C# 6

Now try navigating to Project => Projec Properties again. This time a new tab should open up. On the left click Application if it is not highlightet. Here under Target framework choose .Net Framework 4.6. Visual Studio will tell you, that it wants to restart. Let it!

Next click Build on the lefthand list and finally proclaim yourself an advanced user by klicking the Advanced-button in the bottom right of the tab.

Another window should pop up, finally containing a setting for the utilized C# Language Version. Just change it from C# 4.0 to C# 6.0, click Ok and after a restart of VS everything should work fine.

 

Summary:

  1. In Unity:
    Edit => Project Settings => Player 

    change Scripting Runtime Version from Stable (.Net 3.5 Equivalent) to Experimental (.Net 4.6 Equivalent)
    Ok
  2. In Visual Studio:
    Tools => Options => Tools for Unity
    set Access to project properties to true
    Ok
  3. In Visual Studio:
    Project => Project Properties => Application
    as Target framework choose Net Framework 4.6.
    klick  Ok and agree to restart Visual Studio
    Project => Project Properties => Build => Advaned
    change Language Version to C# 6.0
    Ok
    restart Visual Studio

Leave a comment