Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

About dotnet 6

Discussion in '2017.1 Beta' started by laurentlavigne, Jul 3, 2017.

  1. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    3,166
    So I read that 2017 has an upgrade to dotnet. I know nothing about that new version since I learned c# on unity (jab jab) so can someone tell me how to make this code more succinct now? (something to do withe ?= or ?.)

    Code (CSharp):
    1.         if (Time.time > timer)
    2.         {
    3.             timer = Time.time + 1 / DPS;
    4.             var rb = hit.rigidbody;
    5.             if (rb)
    6.             {
    7.                 var unit = rb.GetComponent<Unit> ();
    8.                 if (unit)
    9.                     unit._health -= 1;
    10.             }
    11.         }
     
  2. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    You can use null propagation to condense those checks to a single line.

    Code (CSharp):
    1. if (Time.time > timer)
    2. {
    3.     timer = Time.time + 1 / DPS;
    4.     hit.rigidbody?.GetComponent<Unit>()?._health -= 1;
    5. }
    But if you don't know enough about those to know where to put them, I'd avoid them until you do. You're better off writing longer, more verbose code until you understand exactly what operators like "?." do.
    You don't get any performance benefits or anything like that.
     
    hippocoder likes this.
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    4,992
    I highly doubt that the ?. operator works with Unity objects, such as Components.

    Just like the ?? operator doesn't work with Unity objects. I filed a bug-report 824765 regarding the ?? operator a while ago:
    QA replied with:
    Therefore I'd be very surprised if Unity supports that ?. operator.
     
  4. pvloon

    pvloon

    Joined:
    Oct 5, 2011
    Posts:
    587
    Indeed it's not safe to use though for a slightly different reason. == null is override for destroyed objects to check whether they are destroyed. GetComponent<> doesn't return destroyed objects though.

    GetComponent<>() could just return an actual null. However, for the special 'Component could not be found' exception, in editor it returns a special object with an overridden == for null.

    This is a bit of a mess and unity seems to be conflicted about it too:

    https://gist.github.com/lucasmeijer/7475fd40ff6ac34c7c44
    https://blogs.unity3d.com/2014/05/16/custom-operator-should-we-keep-it/

    Personally I'm sad that they didn't fix this in the unity 5 cycle like proposed :( Especially the GetComponent<> wrapper could be removed, saving a ton of allocations in the editor too. Until then forum threads like these will keep popping up I guess!
     
    zyzyx and Peter77 like this.
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,727
    Yeah I never use them, and having someone who just discovered them want to abuse them is a major facepalm moment. This is pretty much the worst possible reason to be interested in upgrading! Clarity everyone!

    Good advice :)
     
  6. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    3,166
    Love it, so concise but...
    you are correct, it doesn't work. I just downloaded 2017 and tested this
    Code (CSharp):
    1.         var v = GetComponent<Camera>()?.GetComponent<Transform>()?.GetComponent<Rigidbody>().velocity;  
    2.  
    spits out that
    Code (CSharp):
    1. MissingComponentException: There is no 'Rigidbody' attached to the "Main Camera" game object, but a script is trying to access it.
    2.  
    I wonder if they could add an overload to the ?. operator to make this thing work.
     
  7. pvloon

    pvloon

    Joined:
    Oct 5, 2011
    Posts:
    587
    Not without modifying the C# compiler unfortunately. That might not actually be too far out of reach with modern mono and Roslyn though, buuuut that'll be a while
     
    laurentlavigne likes this.
  8. Seto

    Seto

    Joined:
    Oct 10, 2010
    Posts:
    230
    Your code is incorrect though. You miss the ? after GetComponent<Rigidbody>(). What is the former two GetComponent for? I think you didn't have a clear learning for c# though. Not just the C# 6.
     
  9. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    Quite right, I forgot that Unity overloads the == operator, which has bitten me on more than one occasion.
    I really hope they do remove the overloaded operator. It's usefulness is limited and I'd rather feel silly for not adding a component once in a while that have permanent hacks in my code base to fix quirks.

    In any case, this is exactly why you shouldn't really use things like ?. until you've firmly understood exactly what they do and where they should be used. This could have easily been made into a hard-to-track bug in your project.
     
unityunity