9
\$\begingroup\$

Why can't Unity work with C# 6 code? It always gives me compiler errors. Here are some code examples:

using static System.Convert;
using static System.Environment;

$"€{punten}{NewLine}€{Money}{NewLine}€{KilledEnemies}{NewLine}€{bonus}{NewLine}€{total}";

I use Visual Studio as my code editor and build with no errors. If I press play in Unity it won't build.

\$\endgroup\$
  • \$\begingroup\$ Search for unity .net profile upgrade on Google \$\endgroup\$ – matth Mar 13 '17 at 12:23
18
\$\begingroup\$

Unity uses an old version of Mono runtime which is based on .NET3.5. It uses something between C# 3 and 4 in terms of features.

I found this by googling around. Not sure if it works but might be something worth investigating.

\$\endgroup\$
  • \$\begingroup\$ It's worth noting that there are plans on upgrading to a newer .NET profile some time in the future (unity3d.com/unity/roadmap). However, given the prerequisites stated in the roadmap we'll see that happen only in late 2016 at the very earliest, most likely not until sometime in 2017. \$\endgroup\$ – evilcandybag Jan 4 '16 at 10:19
  • 7
    \$\begingroup\$ The link in this answer rotted away and now returns a 404 :( \$\endgroup\$ – ashes999 Jul 1 '16 at 8:26
  • 7
    \$\begingroup\$ This is the reason why people shouldn't post links on stackexchange without providing a synopsis. \$\endgroup\$ – Philipp Sep 28 '16 at 10:37
  • \$\begingroup\$ Per (forum.unity3d.com/threads/…) that tool is no longer developed/supported. An alternative is (bitbucket.org/alexzzzz/unity-c-5.0-and-6.0-integration/src), which I do personally use, though it has flaws (like Visual Studio forgetting about it), or look to @BenAdams answer, below, for better news. \$\endgroup\$ – Khale_Kitha Sep 30 '16 at 13:16
6
\$\begingroup\$

Unity Technologies have announced that Unity is upgrading to 4.6+. It is available through beta testing now.

\$\endgroup\$
4
\$\begingroup\$

This video on YouTube demonstrates how someone was able to get C# 6 syntax working on Unity by creating a new project, changing the target .Net Framework back to 3.5, setting the build and release output directories to the Unity project assets folder, and adding a reference to the UnityEngine dll. Debugging seems to function the same.

\$\endgroup\$
2
\$\begingroup\$

The compiler in Visual Studio has more features than the compiler in Unity which means that some code (especially newer c# features) will give an error in Unity, but not in Visual Studio.

You can however change your target framework to match the one that is closest to what Unity uses in your editor. I have done this in Xamarin when developing libraries for Unity, but it looks like it is possible in Visual Studio as well.

In Visual Studio, in the DLL project's properties, set the Target framework property to the Unity framework version you're using. This is the Unity Base Class Library that matches the API compatibility that your project targets, such as the Unity full, micro, or web base class libraries. This prevents your DLL from calling framework methods that exist in other frameworks or compatibility levels, but which might not exist in the Unity framework version you're using.

https://msdn.microsoft.com/en-us/library/dn940020.aspx

\$\endgroup\$
  • \$\begingroup\$ You might want to point out that using a different target framework can cause problems when building for other platforms than Windows. \$\endgroup\$ – Philipp Jul 1 '16 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.