{"id":443,"date":"2019-04-03T00:34:07","date_gmt":"2019-04-03T00:34:07","guid":{"rendered":"https:\/\/www.micha.name\/blog\/?p=443"},"modified":"2019-04-03T00:34:07","modified_gmt":"2019-04-03T00:34:07","slug":"gotcha-when-testing-unity-objects-for-null-in-c","status":"publish","type":"post","link":"https:\/\/www.micha.name\/blog\/2019\/04\/03\/gotcha-when-testing-unity-objects-for-null-in-c\/","title":{"rendered":"Gotcha when testing Unity Objects for null in C#"},"content":{"rendered":"<p>Unity has overloaded the &#8216;==&#8217; operator, specifically, checking objects against &#8220;null&#8221; might not always do what you expect. More information can be found <a href=\"https:\/\/blogs.unity3d.com\/2014\/05\/16\/custom-operator-should-we-keep-it\/\">in this blog post from Unity<\/a>.<\/p>\n<p>Specifically, if a Unity Object (as opposed to a C# object) gets destroyed, it will be set to a &#8220;fake null&#8221; object.\u00a0 In this case, the following statements are true:<\/p>\n<pre lang=\"csharp\" line=\"1\" highlight=\"1\">    obj == null          \/\/ true\r\n    obj is null          \/\/ false\r\n    (object)obj == null  \/\/ false\r\n    obj ?? value         \/\/ obj\r\n<\/pre>\n<p>Note the different results between lines 1 and\u00a0 2 &amp; 3.<\/p>\n<p>If obj truly is null, then the results are as expected:<\/p>\n<pre lang=\"csharp\" line=\"1\">    obj == null          \/\/ true\r\n    obj is null          \/\/ true\r\n    (object)obj == null  \/\/ true\r\n    obj ?? value         \/\/ value\r\n<\/pre>\n<p>It may be more readable to provide an Extension Method to UnityObject:<\/p>\n<pre lang=\"csharp\" escaped=\"true\">    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ Extension Method on Unity Objects returning whether the object really is null.\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ Unity overloads the '==' operator so that it returns true on both null references\r\n    \/\/\/ as well as references to destroyed objects. This function only returns true if\r\n    \/\/\/ the reference truly is null, and returns false for \"fake null\" objects.\r\n    public static bool IsTrueNull(this UnityEngine.Object ob)\r\n    {\r\n        return (object)ob == null;\r\n    }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Unity has overloaded the &#8216;==&#8217; operator, specifically, checking objects against &#8220;null&#8221; 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 &#8220;fake null&#8221; object.\u00a0 In this case, the following statements are true: obj == null \/\/ true obj is null \/\/ false (object)obj == null \/\/ false obj ?? value \/\/ obj Note the different results between lines 1 and\u00a0 2 &amp; 3. If obj truly is null, then the results are as expected: 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: \/\/\/ &lt;summary&gt; \/\/\/ Extension Method on Unity Objects returning whether the object really is null. \/\/\/ &lt;\/summary&gt; \/\/\/ Unity overloads the &#8216;==&#8217; 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 &#8220;fake null&#8221; objects. public static bool IsTrueNull(this UnityEngine.Object ob) { return [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[27],"tags":[80,79,28,78],"_links":{"self":[{"href":"https:\/\/www.micha.name\/blog\/wp-json\/wp\/v2\/posts\/443"}],"collection":[{"href":"https:\/\/www.micha.name\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.micha.name\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.micha.name\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.micha.name\/blog\/wp-json\/wp\/v2\/comments?post=443"}],"version-history":[{"count":2,"href":"https:\/\/www.micha.name\/blog\/wp-json\/wp\/v2\/posts\/443\/revisions"}],"predecessor-version":[{"id":445,"href":"https:\/\/www.micha.name\/blog\/wp-json\/wp\/v2\/posts\/443\/revisions\/445"}],"wp:attachment":[{"href":"https:\/\/www.micha.name\/blog\/wp-json\/wp\/v2\/media?parent=443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.micha.name\/blog\/wp-json\/wp\/v2\/categories?post=443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.micha.name\/blog\/wp-json\/wp\/v2\/tags?post=443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}