Synchronizing Unity Visual Studio C # Version

I've just started using delegates and events in my Unity game to update the labels when needed, instead of updating the labels every frame, although in most cases there is no change.

To make sure static event call

public static event OnSomething onSomething;

      

There are actually listeners, I am using

if (onSomething!= null) {
    onSomething();
}

      

This is just a convenience issue, but Visual Studio 14 (2015) says it can be simplified to

onSomething?.Invoke();

      

which I also like more because I am familiar with nullables and would like to use them, but if I change the code to suggestion, Unity says

error CS1644: Feature `null propagating operator' cannot be used, because it is not part of the C# 4.0 language specification.

      

So, is there a way to make the Unity project use v4.0 from C #, or can I tell Visual Studio that the project is using an older version of C #, so it stops these suggestions?

Edit: Little clarification: I don't need any particular version of C #. I want to use the newest version to use these "cool new features" unless there is a primary reason not to use the newest version.

+3


source to share


3 answers


I don't need to use any specific version of C #. I want to use the newest version to use these "cool new features" as there is no primary reason not to use the newest version.

Unity doesn't officially support anything above .NET 3. Although they are currently working on it. They want to support .NET 4 first and then another version of .NET.

Can you use the latest C # in Unity?

Yes

There is a project dedicated to this on butbucket called "Unity C # 5.0 and 6.0 Integration". You can get it here . It allows you to do just that, and the instructions required to use it are posted on this site.

This works on iOS, Android, Windows, and macOS, but I wouldn't use this to release my game simply because it was not officially supported.




As for your attemp for use

onSomething?.Invoke();

      

I suggest you do not use this in your unity. You will run into a lot of problems if you get into the habit of using this.

This is because Unity uses a custom equality operator for Components

and GameObjects

. It won't work if you try to do it on Component

or GameObject

. You will simply create so many problems for yourself. Here's an example of one and here is another reason to stay away from Null propagation when working in Unity.

EDIT

Unity 2017 now supports 4.6 which will allow this feature to work, but I still suggest you test and experiment with this to make sure there isn't a big runtime issue like this before releasing the game.

+4


source


I would like to point out that C # 4.0 has nothing to do with .NET 3.5, 4.0, 4.5, etc. Its Visual Studio version and compiler. The reason you get a recommendation from Visual Studio is because you are using 2015 or newer.

I got tired of this suggestion myself and found a way to turn it off, unfortunately it will turn it off on ALL design decisions you have ever worked with. Please note that this is specific to Visual Studio 2015 and I have an Enterprise Edition, this has NOT been tested on the Community Edition so not sure if it works for this, or if the file locations might be elsewhere, but this is how I disabled:

Visual Studio 2015 uses a set of rules for code analysis, now you can change these rules for a project based project by right clicking your project, selecting properties, then go to the Code Analysis tab at the bottom, but Unity every time you add C # script into your game, overwrite all changes made to the project file. So, I did the following:

in folder:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\Rule Sets

      

are all the rulesets that Visual Studio 2015 uses to analyze code if you edit a file:



MinimumRecommendedRules.ruleset

      

and add:

<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.Features" RuleNamespace="Microsoft.CodeAnalysis.CSharp.Features">
    <Rule Id="IDE1005" Action="None" />
</Rules>

      

until the end before closing </RuleSet>

it will turn off the worldwide offer for all projects.

Hope this helps someone who is also sick of the suggestion in Unity projects.

0


source


Unity recently released a preview of .NET 4.6, which can be enabled by going to Edit> Project Settings> Player and Run Script Version as Experimental (equivalent to .NET 4.6).

You will need at least Unity 2017.1 to use this feature.

I've added a link with more information: https://docs.unity3d.com/Manual/ScriptingRuntimeUpgrade.html

0


source







All Articles