Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity Visual Studio C# version synchronization

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.

like image 475
Tavados Avatar asked Mar 22 '17 15:03

Tavados


1 Answers

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.

like image 183
Programmer Avatar answered Oct 02 '22 14:10

Programmer