{"id":883,"date":"2025-07-24T18:49:57","date_gmt":"2025-07-24T18:49:57","guid":{"rendered":"https:\/\/docs.sisus.co\/init-args\/?p=883"},"modified":"2025-07-25T06:38:47","modified_gmt":"2025-07-25T06:38:47","slug":"null-argument-guard","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/initializers\/null-argument-guard\/","title":{"rendered":"7. Null Argument Guard"},"content":{"rendered":"<h1>Null Argument Guard<\/h1>\n<p>The <strong>Null Argument Guard<\/strong> is a feature that can automatically inform you about missing Init arguments that any of your components might have. The feature consists of three distinct parts:<\/p>\n<ol>\n<li><strong>Inspector Warnings<\/strong> &#8211; Warnings shown in the Editor when you view a component containing missing Init arguments in the Inspector Window.<\/li>\n<li><strong>Edit Mode Warnings<\/strong> &#8211; Warnings logged into the Console Window when components with missing Init arguments are loaded in Edit Mode.<\/li>\n<li><strong>Runtime Exceptions<\/strong> &#8211; Exceptions thrown when components with missing Init arguments are initialized at runtime.<\/li>\n<\/ol>\n<h2>Inspector Warnings<\/h2>\n<p>An Init section listing all the services that a client depends is drawn in the Inspector for all components that derive from <a href=\"https:\/\/docs.sisus.co\/init-args\/clients\/monobehaviour-t\/\">MonoBehaviour&lt;T&#8230;&gt;<\/a>, implement <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/iinitializable\/\">IInitializable&lt;T&#8230;&gt;<\/a>, or have an <a href=\"https:\/\/docs.sisus.co\/init-args\/initializers\/initializer\/\">Initializer<\/a> attached to them.<\/p>\n<p>When one or more of the Init arguments that one such client component depends on are missing, the Null Argument Guard will by default display a warning about it, and highlight the relevant fields in a yellow color.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-887\" src=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2025\/07\/null-argument-guard.png\" alt=\"\" width=\"416\" height=\"214\" srcset=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2025\/07\/null-argument-guard.png 416w, https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2025\/07\/null-argument-guard-300x154.png 300w\" sizes=\"(max-width: 416px) 100vw, 416px\" \/><\/p>\n<h2>Edit Mode Warnings<\/h2>\n<p>When an initializer is attached to a component, you can also have warnings be printed to the Console Window in Edit Mode if the component is loaded with some of its Init arguments being missing.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-889\" src=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2025\/07\/edit-mode-warnings.png\" alt=\"\" width=\"533\" height=\"89\" srcset=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2025\/07\/edit-mode-warnings.png 533w, https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2025\/07\/edit-mode-warnings-300x50.png 300w\" sizes=\"(max-width: 533px) 100vw, 533px\" \/><\/p>\n<h3>Configuring Edit Mode Warnings<\/h3>\n<p>Both Inspector Warnings and Edit Mode Warnings are controlled using the same settings.<\/p>\n<h4>Null Argument Guard Icon<\/h4>\n<p>You can toggle <strong>Edit Mode Warnings<\/strong> on and off using the <strong>Null Argument Guard icon<\/strong> found in the top-right corner of the Init section.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-888\" src=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2025\/07\/disabling-edit-mode-warning.gif\" alt=\"\" width=\"412\" height=\"210\" \/><\/p>\n<p>If an Initializer is attached to the component, then the toggling this option will only affect this one particular client. This is because Initializers are used to configure the Init arguments of one particular instance.<\/p>\n<p>If the component does not have an Initializer attached, then the toggling this option affect all instances of the component &#8211; except for those that might have an Initializer attached. In this case the setting will saved in the .meta data of the component whenever possible (if this is not possible for whatever reason, EditorPrefs is used as fallback).<\/p>\n<h4>[Init] Attribute<\/h4>\n<p>It is also possible to add the <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/init-attribute\/\">[Init] attribute<\/a> to a class and assign <em>None<\/em> or <em>RuntimeException<\/em> to its <em>NullArgumentGuard<\/em> property to the disable Edit Mode Warnings for all instances of the class by default.<\/p>\n<pre>[Init(NullArgumentGuard = NullArgumentGuard.RuntimeException)]\r\nclass Player : MonoBehaviour&lt;InputManager, GameManager&gt;\r\n{\r\n   ...<\/pre>\n<h4>Best Practices<\/h4>\n<p>Edit Mode Warnings can be very useful for helping make sure you catch any broken references and improperly registered services as soon as possible.<\/p>\n<p>If you only register globally available services using the <a href=\"https:\/\/docs.sisus.co\/init-args\/services\/service-attribute\/\">[Service] attribute<\/a>, and constrain the availability of local services to the hierarchies of the scenes and prefabs they are attached to, then the Inspector Warnings will almost give you accurate results.<\/p>\n<p>If this fails, you can also avoid having to turn off Edit Mode Warnings entirely, by attaching an Initializer to a component, and assigning <em>Wait For Service<\/em> to the Init argument fields that depend on local services that will only become available at runtime.<\/p>\n<h2><strong>Runtime Exceptions<\/strong><\/h2>\n<p>You can also have an InvalidInitArgumentsException be automatically thrown if components that derives from MonoBehaviour&lt;T&#8230;&gt; or has an Initializer attached don&#8217;t receive all the Init arguments that they depend on at runtime.<\/p>\n<h3><strong>Configuring Runtime Exceptions<\/strong><\/h3>\n<h4>Null Argument Guard Icon<\/h4>\n<p>You can toggle <strong>Runtime Exceptions<\/strong>\u00a0on and off using the <strong>Null Argument Guard icon<\/strong> found in the top-right corner of the Init section.<\/p>\n<h4>[Init] Attribute<\/h4>\n<p>You can also add the <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/init-attribute\/\">[Init] attribute<\/a> to a class and assign <em>None<\/em> or <em>EditModeWarning<\/em>\u00a0to its <em>NullArgumentGuard<\/em> property to the disable Runtime Exceptions for all instances of the class by default.<\/p>\n<pre>[Init(NullArgumentGuard = NullArgumentGuard.None)]\r\nclass Player : MonoBehaviour&lt;InputManager, GameManager&gt;\r\n{\r\n   ...<\/pre>\n<h3><strong>Configuring Wait For Services<\/strong><\/h3>\n<p>By default, client components don&#8217;t immediately throw an exception even if Runtime Exceptions are enabled and they are loaded with some of their Init arguments still being missing.<\/p>\n<p>Instead, they will be disabled, and their initialization delayed for some time, to give local services that the client might depend on some time to load. Currently clients will wait for up to 3 seconds for all their Init arguments to become available before considering it a lost cause, and throwing an exception (if Runtime Exceptions are enabled).<\/p>\n<p>If you would like to have all instances of a particular component class throw an exception immediately, instead of after a delay of 3 seconds, you can add the [Init] attribute to the class and assign <em>false <\/em>to the <em>WaitForServices<\/em> property.<\/p>\n<pre>[Init(WaitForServices = false, NullArgumentGuard = NullArgumentGuard.All)]\r\nclass Player : MonoBehaviour&lt;InputManager, GameManager&gt;\r\n{\r\n   ...<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Null Argument Guard The Null Argument Guard is a feature that can automatically inform you about missing Init arguments that any of your components might have. The feature consists of three distinct parts: Inspector Warnings &#8211; Warnings shown in the Editor when you view a component containing missing Init arguments in the Inspector Window. Edit ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/initializers\/null-argument-guard\/\" 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":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/883"}],"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=883"}],"version-history":[{"count":5,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/883\/revisions"}],"predecessor-version":[{"id":893,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/883\/revisions\/893"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}