{"id":215,"date":"2021-12-27T16:04:05","date_gmt":"2021-12-27T16:04:05","guid":{"rendered":"https:\/\/docs.sisus.co\/init-args\/?p=215"},"modified":"2025-07-14T19:35:16","modified_gmt":"2025-07-14T19:35:16","slug":"null-nullorinactive","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/reference\/null-nullorinactive\/","title":{"rendered":"Null \/ NullOrInactive"},"content":{"rendered":"<p>Init(args) has been designed with inversion of control in mind and aims to make it as easy as possible to work with interfaces instead of specific classes in Unity.<\/p>\n<p>One pain point when using interfaces in Unity is that checking them for null can be problematic. This is because in addition to being actually null, Objects in Unity can also be\u00a0<a class=\"externalLink\" href=\"https:\/\/docs.unity3d.com\/ScriptReference\/Object.Destroy.html\" target=\"_blank\" rel=\"nofollow noopener\">destroyed<\/a>. You usually don&#8217;t even notice the difference when working with specific classes directly, because the Object class has a custom\u00a0<a class=\"externalLink\" href=\"https:\/\/docs.unity3d.com\/ScriptReference\/Object-operator_Object.html\" target=\"_blank\" rel=\"nofollow noopener\">== operator<\/a>\u00a0which internally also checks whether the Object has been destroyed. However when an Object has been assigned to an interface type variable and this is compared against null, the overloaded == operator does not get used. This can result in null reference exceptions when trying to access the members of a destroyed Object.<\/p>\n<div class=\"bbCodeBlock bbCodeCode\">\n<div class=\"code\">\n<pre data-redactor=\"1\">Player player = FindAnyObjectByType&lt;Player&gt;();\r\nIPlayer iplayer = player as IPlayer;\r\n\r\nDestroy(player);\r\n\r\nDebug.Log(player == null);\u00a0 \/\/ Prints true\r\nDebug.Log(iplayer == null); \/\/ Prints false<\/pre>\n<div data-redactor=\"1\">To help with this problem in version a property has been added into every base class in Init(args): <strong>Null<\/strong>. When an interface type variable is compared against this Null property it functions just like the overloaded == operator in the Object class and returns true when the instance has been destroyed.<\/div>\n<\/div>\n<\/div>\n<pre data-redactor=\"1\">Player player = FindAnyObjectByType&lt;Player&gt;();\r\nIPlayer iplayer = player as IPlayer;\r\n\r\nDestroy(player);\r\n\r\nDebug.Log(player == null);\u00a0 \/\/ Prints true\r\nDebug.Log(iplayer == null); \/\/ Prints false\r\nDebug.Log(iplayer == Null); \/\/ Prints true<\/pre>\n<p>It is also possible to utilize this safe null checking inside classes that don&#8217;t derive from any of the base classes in Init(args) by importing the static members of the NullExtensions class.<\/p>\n<pre data-redactor=\"1\">using UnityEngine;\r\nusing static Sisus.NullExtensions;\r\n\r\npublic static class PlainOldClass\r\n{\r\n \u00a0\u00a0 public static void Example()\r\n \u00a0\u00a0 {\r\n \u00a0 \u00a0 \u00a0\u00a0 Player player = Object.FindAnyObjectByType&lt;Player&gt;();\r\n \u00a0 \u00a0 \u00a0\u00a0 IPlayer iplayer = player as IPlayer;\r\n\r\n   \u00a0 \u00a0\u00a0 Object.Destroy(player);\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0\u00a0 Debug.Log(player == null);\u00a0 \/\/ Prints true\r\n \u00a0 \u00a0 \u00a0\u00a0 Debug.Log(iplayer == null); \/\/ Prints false\r\n \u00a0 \u00a0 \u00a0\u00a0 Debug.Log(iplayer == Null); \/\/ Prints true\r\n \u00a0\u00a0 }\r\n}<\/pre>\n<p>In addition to the Null property there&#8217;s also a new NullOrInactive property. This functions identically to the Null property but also checks if the Object is a component on an inactive GameObject.<\/p>\n<pre data-redactor=\"1\">Player player = FindAnyObjectByType&lt;Player&gt;();\r\nIPlayer iplayer = player as IPlayer;\r\n\r\nplayer.gameObject.SetActive(false);\r\n\r\nDebug.Log(player.gameObject.activeInHierarchy); \/\/ Prints true\r\nDebug.Log(iplayer == NullOrInactive); \/\/ Prints true<\/pre>\n<p>This functionality can be particularly useful when utilizing\u00a0<a class=\"externalLink\" href=\"https:\/\/docs.unity3d.com\/2021.1\/Documentation\/ScriptReference\/Pool.ObjectPool_1.html\" target=\"_blank\" rel=\"nofollow noopener\">Object Pooling<\/a>\u00a0and an inactive GameObject is meant to represent an object which doesn&#8217;t exist.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Init(args) has been designed with inversion of control in mind and aims to make it as easy as possible to work with interfaces instead of specific classes in Unity. One pain point when using interfaces in Unity is that checking them for null can be problematic. This is because in addition to being actually null, ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/null-nullorinactive\/\" title=\"read more\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/215"}],"collection":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":6,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":609,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/215\/revisions\/609"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}