• Gotcha when testing Unity Objects for null in C#

    Unity has overloaded the ‘==’ operator, specifically, checking objects against “null” might not always do what you expect. More information can be found in this blog post from Unity.

    Specifically, if a Unity Object (as opposed to a C# object) gets destroyed, it will be set to a “fake null” object.  In this case, the following statements are true:

    1
    2
    3
    4
    
        obj == null          // true    obj is null          // false
        (object)obj == null  // false
        obj ?? value         // obj

    Note the different results between lines 1 and  2 & 3.

    If obj truly is null, then the results are as expected:

    1
    2
    3
    4
    
        obj == null          // true
        obj is null          // true
        (object)obj == null  // true
        obj ?? value         // value

    It may be more readable to provide an Extension Method to UnityObject:

        /// <summary>
        /// Extension Method on Unity Objects returning whether the object really is null.
        /// </summary>
        /// Unity overloads the '==' operator so that it returns true on both null references
        /// as well as references to destroyed objects. This function only returns true if
        /// the reference truly is null, and returns false for "fake null" objects.
        public static bool IsTrueNull(this UnityEngine.Object ob)
        {
            return (object)ob == null;
        }

  • Last minute ski trip – first afternoon

    So I eventually made it to the Chalet and met up with FiSH and John after checking in. Brief spot of lunch, sort out ski hire and lift pass, and a short while later we’re heading up the mountain for my first attempt at nreaking bones in 14 years or so.

    With a shout of “No guts, no glory” I launched myself down the slope. The first turn was a bit iffy (I had completely forgotten how to) but then muscle memory kicked.in and it was all good.

    Had a great arvo skiing even hitting some red runs. Weather and surroundings just gorgeous and surprisingly empty slopes.

    After the lifts closed we headed back to the Chalet for a warm-up in the Onsen before heading to dinner at a local Izakaya. Highlight was horse sashimi – delicious and so tender it melted in your mouth.

    Then back to the Chalet for and early night.. brekky at 7:30!


  • Last minute ski trip – departure

    So a mate of mine, FiSH, texted me around 5pm saying the skiing was great up in Yamanouchi and that it would be great if I could join fir the weekend.

    A few clicks later and I had a reservation at Chalet Shiga, the place he’s staying at. Then off.to ask my boss if I could.take the Friday off – to which he surprisingly agreed 😀

    Next priority – down to Bar82 where I was meeting up with some old friends. A few beers later and it’s to the JR counter to get a Shinkansen ticket to Nagano.

    Back to the apartment, pack, shower, set alarm, sleep. Too few hours (6) later and I’m up – 10 minutes before the alarm!

    A short while and a pic of sunrise later and I’m on the Yamanote line bound for Tokyo where the Shinkansen stops. In typical German style I’m on the platform 40 minutes too early – time to write a blog entry and have a coffee!