Reputation: 2298
I just started to use delegates and events in my Unity game to update labels when appropriate, instead of updating the labels every frame, although most of the time no changes occur.
To make sure, that a static event call of
public static event OnSomething onSomething;
actually has listeners, I use
if (onSomething!= null) {
onSomething();
}
It's just a convenience problem, but Visual Studio 14 (2015) says, that this can be simplified to
onSomething?.Invoke();
which I also like more, because I'm familiar with nullables and would like to use them, but if I do change the code to the 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 a Unity project use v4.0 of C#, or can I tell Visual Studio, that the project uses an older version of C#, so it stops these suggestions?
Edit: Little clarification: I don't neccessarily want to use any specific C# version. I want to use the newest version possible to use these "cool new features", as long as there is no major reason not to use the newest version.
Upvotes: 5
Views: 6407
Reputation: 125315
I don't neccessarily want to use any specific C# version. I want to use the newest version possible to use these "cool new features", as long as there is no major reason not to use the newest version.
Unity does not officially support anything above .NET 3. Although, they are currently working on this. They want to support .NET 4 first then other .NET version.
Can you use newest 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 lets you do exactly this and the instructions required to use this is posted on that site.
This works on iOS, Android, Windows and MacOS but I wouldn't use this to release my game simply because it's not officially supported.
As for your attemp to use
onSomething?.Invoke();
I suggest you don't use that in your Unity at-all. You will run into many problems if you form a habit of using that.
The reason for this is because Unity uses custom equality operator for Components
and GameObjects
. It won't work if you try to do this on a Component
or GameObject
. You will simply create so many problems for your self. Here is 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 will still suggest you test and experiment a lot with this to make sure there is no big problem such as this during run-time before releasing your game with it.
Upvotes: 5
Reputation: 107
Unity has recently released a preview version of .NET 4.6 that can be enabled by going to: Edit > Project Settings > Player and setting Scripting Runtime Version as 'Experimental (.NET 4.6 Equivalent)'.
You'll need at least Unity 2017.1 to use this feature however.
I've included a link with some more information: https://docs.unity3d.com/Manual/ScriptingRuntimeUpgrade.html
Upvotes: 1
Reputation: 700
I would like to point out that C#4.0 has nothing to do with .NET 3.5, 4.0, 4.5 etc. Its the version of Visual Studio and the compiler. The reason your getting the recommendation from Visual Studio is because your using 2015 or newer.
I myself got sick of this suggestion and found a way to disable it, unfortunately it will disable it on ALL projects solutions that you ever work with. Note this is specific to Visual Studio 2015 and I have Enterprise Edition this has NOT been tested on Community Edition so not sure if it works for that one or if the location of the files may be in a different spot as well but here is how I disabled it:
Visual Studio 2015 uses a rule set for the code analysis, now you can change these rules on a project per project basis by right clicking your project choosing properties then go to the Code Analysis Tab at the bottom, but Unity every time you add a C# script to your game will overwrite any changes you make to the project file. So what I did was as follows
in the folder:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\Rule Sets
is all the rule sets that visual studio 2015 uses for code analysis, if you edit the file:
MinimumRecommendedRules.ruleset
and add:
<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.Features" RuleNamespace="Microsoft.CodeAnalysis.CSharp.Features">
<Rule Id="IDE1005" Action="None" />
</Rules>
to the end just before the closing </RuleSet>
it will disable the suggestion globally on all projects.
Hope this helps someone else who is also sick of the suggestion in Unity projects
Upvotes: 0