Search Unity

Unity not allowing null-propogation (?.) operator (C#)

Discussion in 'Scripting' started by TerabyteTim, Jul 31, 2015.

  1. TerabyteTim

    TerabyteTim

    Joined:
    Oct 20, 2011
    Posts:
    115
    So I'm using a bit of code that utilizes null-propogation for a check, like this:
    Code (CSharp):
    1. possiblyNullVar?.GetComponent<Selectable>();
    Visual Studio says this code is fine, as I would expect. However when I go in to Unity, the console spits out the error "Unexpected symbol '?'".

    Does Unity not support null-propogation? Is there a way for me to enable this? Or do I simply have to just not use it?
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Nope, Currently unity supports .net 3.5 I believe
     
    Kiwasi likes this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    C# 3.0 is only supported (.net 3.5, CLR 2.0 compliant... I know, a ton of stupid acronyms)

    This thread talks about getting C# 6.0 syntactic support directly in unity:
    http://forum.unity3d.com/threads/c-6-0.314297/page-2#post-2207782

    Note... this will only get you C# 6.0 syntactic support. This means any syntax sugar (like null-propogation) will work. You are still stuck on .net 3.5/CLR 2.0 compliance as the framework goes.

    Note, if you write and compile your code in Visual Studio to a dll, you can get the support there as well easily. Just make sure the dll you compile still targets CLR 2.0.
     
  4. TerabyteTim

    TerabyteTim

    Joined:
    Oct 20, 2011
    Posts:
    115
    Hmm, that sucks that Unity is still stuck on C# 3.0, seems kind of odd.

    Not sure if I want too have to include a plugin with every project I do just so I can utilize C# 6.0 syntax. Does it provide much of a performance benefit? Have you used the plugin mentioned in that thread at all?
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    Not a whole lot of performance benefit really, as it still compiles to the same kind of CIL that targets the same mono runtime which is CLR 2.0 compliant.

    There might be some slight nuance differences in the what CIL is generated by the newer compiler that could result in some faster code... but could also result in some slower code as well.

    The reason for all of this is because Unity uses Mono, but that means they need Xamarin to compile for things like Android and iOS. And Xamarin wants a larger license fee to upgrade to the newest version of Mono, which Unity isn't willing to shell out. So instead we're stuck on an older version of Mono until Unity figures out how it wants to move forward (they're currently developing a compiler which creates c code out of CIL they call il2cpp, it's used for the webgl builds in unity 5 currently).
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    Honestly I don't really miss any of the C# 6.0 syntax sugar. I get by just fine with out most of it.

    It's the newer runtime/framework stuff that I wish I had my hands on. And the newer garbage collector in the latest version of Mono... that I REALLY wish we could get our hands on. But alas... business politics... I'm not quite sure what I think of this il2cpp move that they're heading towards right now, but if they can resolve the garbage collector issues and maintain FULL .net 3.5 support, I'll be content with it.