{"id":1128,"date":"2026-08-04T18:10:14","date_gmt":"2026-08-04T18:10:14","guid":{"rendered":"https:\/\/docs.sisus.co\/init-args\/?p=1128"},"modified":"2026-08-04T18:17:15","modified_gmt":"2026-08-04T18:17:15","slug":"init-order-optimization","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/initializers\/init-order-optimization\/","title":{"rendered":"8. Init Order Optimization"},"content":{"rendered":"<p>Init(args) can automatically optimize the order in which components that are loaded as part of the same scene or prefab are initialized, based on their dependencies.<\/p>\n<h3>Why Init Order Matters<\/h3>\n<p>Imagine you have a component called Client which needs to execute a method on another component called Service when it&#8217;s being enabled:<\/p>\n<pre><span class=\"lines-line\"><span class=\"hljs-keyword\">class<\/span> Client : <span class=\"hljs-title\">MonoBehaviour<\/span>&lt;Service&gt;\r\n<\/span><span class=\"lines-line\">{\r\n<\/span><span class=\"lines-line\">   Service service;\r\n\r\n<\/span><span class=\"lines-line\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">   protected<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Init<\/span>(Service<span class=\"hljs-params\"> service<\/span>)<\/span> =&gt; <span class=\"hljs-keyword\">this<\/span>.<span class=\"hljs-function\"><span class=\"hljs-params\">service<\/span><\/span> = <span class=\"hljs-function\"><span class=\"hljs-params\">service<\/span><\/span>;\r\n\r\n<\/span><span class=\"lines-line\"><span class=\"hljs-function\">   <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">OnEnable<\/span>()<\/span> =&gt; <span class=\"hljs-function\"><span class=\"hljs-params\">service.DoSomething();<\/span><\/span>\r\n<\/span><span class=\"lines-line\">}\r\n<\/span><\/pre>\n<p>Is this safe to do? What if Client&#8217;s OnEnable happens to get executed before Service&#8217;s Awake and OnEnable events do, and Service.DoSomething actually can&#8217;t safely be used yet?<\/p>\n<pre><span class=\"lines-line\">class Service : MonoBehaviour\r\n{\r\n    bool initialized;\r\n    void Awake() =&gt; Initialize();\r\n\r\n    public void <span class=\"hljs-function\"><span class=\"hljs-params\">DoSomething()\r\n<\/span><\/span>    {\r\n        if(!initialized) throw new Exception(\"Not initialized yet!\");\r\n\r\n        ...\r\n    }\r\n\r\n    void Initialize()\r\n    {\r\n        initialized = true;\r\n\r\n        ...\r\n    }\r\n}<\/span><\/pre>\n<p>Normally, if you were to connect the two components using serialized fields or GetComponent in Unity, they would get loaded in arbitrary order by default &#8211; meaning, Client might or might not throw an exception when its tested at runtime. And perhaps it might even work when tested in the Editor, but fail in your release build.<\/p>\n<h3>Solution: Automatic Init Order Optimization<\/h3>\n<p>Init(args) can automatically optimize the initialization order of all components that derive from <code>MonoBehaviour&lt;T...&gt;<\/code> and execute code during the <code>Awake<\/code> or <code>OnEnable<\/code> events, based on the components that they depend on.<\/p>\n<p>So if A depends on B, and A executes any code during <code>Awake<\/code> or <code>OnEnable<\/code>, then Init(args) will automatically ensure B&#8217;s <code>Awake<\/code> and <code>OnEnable<\/code> methods are executed before A&#8217;s are.<\/p>\n<h3>Configuration<\/h3>\n<p>Automatic Init Order optimization can be customized or turned off in <code>Project Settings &gt; Init(args)<\/code>.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-1129\" src=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2026\/08\/init-order-optimization.png\" alt=\"\" width=\"578\" height=\"410\" srcset=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2026\/08\/init-order-optimization.png 578w, https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2026\/08\/init-order-optimization-300x213.png 300w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/><\/p>\n<h4>Include All<\/h4>\n<p><strong>The Include<\/strong> All toggle determines whether Init(args) will optimize the script execution orders of all components belonging the category, whenever either the <strong>Optimize All<\/strong> button is pressed manually or when the <strong>Optimize Automatically<\/strong> option is enabled.<\/p>\n<h4>Execution Order Range<\/h4>\n<p>Determines the minimum and maximum script execution order for all scripts belonging to the category.<\/p>\n<p>The default script execution order of scripts is 0, and scripts with negative values get executed before that, and scripts with positive values get executed after that.<\/p>\n<h4>Optimize All<\/h4>\n<p>Pressing the <strong>Optimize All<\/strong> button causes the script execution order of all scripts belonging to all categories with <strong>Include All<\/strong> enabled to get re-optimized.<\/p>\n<h4>Reset All<\/h4>\n<p>Pressing the <strong>Reset All<\/strong> button will cause Script Execution Order of all scripts that Init(args) automatically optimizes back to their default state (0).<\/p>\n<p><strong>Tip<\/strong>: You may want to use this after changing the <strong>Execution Order Range<\/strong> for a category, so that script execution orders of scripts get spread out evenly along the whole range.<\/p>\n<h4>Optimize Automatically<\/h4>\n<p>When enabled, Init(args) will automatically detect when new scripts are being imported and trigger script execution orders to be re-optimized as a result.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Init(args) can automatically optimize the order in which components that are loaded as part of the same scene or prefab are initialized, based on their dependencies. Why Init Order Matters Imagine you have a component called Client which needs to execute a method on another component called Service when it&#8217;s being enabled: class Client : ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/initializers\/init-order-optimization\/\" 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\/1128"}],"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=1128"}],"version-history":[{"count":4,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/1128\/revisions"}],"predecessor-version":[{"id":1133,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/1128\/revisions\/1133"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=1128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=1128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=1128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}