{"id":418,"date":"2024-09-20T18:41:42","date_gmt":"2024-09-20T18:41:42","guid":{"rendered":"https:\/\/docs.sisus.co\/init-args\/?p=418"},"modified":"2025-07-14T19:31:54","modified_gmt":"2025-07-14T19:31:54","slug":"init-in-edit-mode","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/reference\/init-in-edit-mode\/","title":{"rendered":"[InitInEditMode] attribute"},"content":{"rendered":"<p>Add this attribute to a component to have the arguments accepted by it be automatically gathered and passed to its Init function in Edit Mode, whenever any objects in the same scene or prefab that contains the component are modified.<\/p>\n<p>This attribute supports any components that implement <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/iinitializable\/\">IInitializable&lt;T&gt;<\/a>, which includes all classes that derive from <a href=\"https:\/\/docs.sisus.co\/init-args\/features\/monobehaviour-t\/\">MonoBehaviour&lt;T&gt;<\/a>.<\/p>\n<p>Dependencies for the component are automatically retrieved from the scene hierarchy, relative to the GameObject that holds the component with the attribute.<\/p>\n<p>You can specify from where Object arguments should be searched, or use the default search mode, which tries to pick a good search mode to use based on the type of the argument:<\/p>\n<ul>\n<li><strong>Transform or GameObject<\/strong>: From.GameObject<\/li>\n<li><strong>Other Component or interface<\/strong>: From.Children or From.Parent or From.SameScene<\/li>\n<li><strong>Other Object<\/strong>: From.Assets<\/li>\n<li><strong>Collection of Component, interface, Transform or GameObject<\/strong>: From.Children\n<ul>\n<li>For example Collider[] or IEnumerable&lt;Transform&gt;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>The found arguments are passed to the component&#8217;s IInitializable&lt;&gt;.Init function,<br \/>\nwhere they can be assigned to serialized fields.<\/p>\n<p>This behaviour only occurs in edit mode and is meant to make it more convenient to add components without needing to assign all Object references manually using the inspector.<\/p>\n<h2>Example<\/h2>\n<p>For example, the following Player component would get its Init method automatically called in Edit Mode with the nearest Collider and AnimatorController components found in the same scene or prefab that holds the component (if both are found):<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\"><span style=\"font-size: small;\"><span style=\"color: #808030;\">[InitInEditMode]<\/span><\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"font-size: small;\"><span style=\"color: #800000;\"><b>public<\/b><\/span> <span style=\"color: #800000;\"><b>class<\/b><\/span> Player <span style=\"color: #808030;\">:<\/span> MonoBehaviour<span style=\"color: #808030;\">&lt;<\/span>Collider<span style=\"color: #808030;\">,<\/span> AnimatorController<span style=\"color: #808030;\">&gt;<\/span><\/span><\/span>\r\n<span style=\"color: #800080;\"><span style=\"font-size: small;\">{<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #808030;\">    <span style=\"font-size: small;\">[<\/span>SerializeField]<\/span><\/span>\r\n<span style=\"color: #000000;\">    <span style=\"font-size: small;\"><span style=\"color: #800000;\"><b>private<\/b><\/span> Collider collider<span style=\"color: #800080;\">;<\/span><\/span><\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #808030;\">    <span style=\"font-size: small;\">[<\/span>SerializeField]<\/span><\/span>\r\n<span style=\"color: #000000;\">    <span style=\"font-size: small;\"><span style=\"color: #800000;\"><b>private<\/b><\/span> AnimatorController animatorController<span style=\"color: #800080;\">;<\/span><\/span><\/span>\r\n\r\n<span style=\"color: #000000;\">    <span style=\"font-size: small;\"><span style=\"color: #800000;\"><b>protected override<\/b><\/span> <span style=\"color: #800000;\"><b>void<\/b><\/span> Init<span style=\"color: #808030;\">(<\/span>Collider collider<span style=\"color: #808030;\">,<\/span><\/span><\/span>\r\n<span style=\"color: #000000;\">                                 <span style=\"font-size: small;\">AnimatorController animatorController<span style=\"color: #808030;\">)<\/span><\/span><\/span>\r\n<span style=\"color: #000000;\">    <span style=\"font-size: small;\"><span style=\"color: #800080;\">{<\/span><\/span><\/span>\r\n<span style=\"color: #000000;\">        <span style=\"font-size: small;\"><span style=\"color: #800000;\"><b>this<\/b><\/span><span style=\"color: #808030;\">.<\/span>collider <span style=\"color: #808030;\">=<\/span> collider<span style=\"color: #800080;\">;<\/span><\/span><\/span>\r\n<span style=\"color: #000000;\">        <span style=\"font-size: small;\"><span style=\"color: #800000;\"><b>this<\/b><\/span><span style=\"color: #808030;\">.<\/span>animatorController <span style=\"color: #808030;\">=<\/span> animatorController<span style=\"color: #800080;\">;<\/span><\/span><\/span>\r\n<span style=\"color: #000000;\">    <span style=\"font-size: small;\"><span style=\"color: #800080;\">}<\/span><\/span><\/span>\r\n<span style=\"color: #800080;\"><span style=\"font-size: small;\">}<\/span><\/span><\/pre>\n<p>By default component argument values are retrieved using the following logic:<\/p>\n<ol>\n<li>First the same GameObject is searched for a component of the required type.<\/li>\n<li>Second all child GameObjects are searched for a component of the required type.<\/li>\n<li>Third all parent GameObjects are searched for a component of the required type.<\/li>\n<li>Lastly all GameObjects in the same scene are searched for a component of the required type.<\/li>\n<\/ol>\n<p>For each required component type the first match that is found using this search order is retrieved and then all the results are injected to the Init method.<\/p>\n<p>It is also possible to manually specify which GameObjects should be searched for each argument. You can for example use the GetOrAddComponent value to try searching for the component in the same GameObject and then automatically adding it to it if no existing instance is found.<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\"><span style=\"color: #808030;\">[InitInEditMode<\/span><span style=\"color: #808030;\">(<\/span>From<span style=\"color: #808030;\">.<\/span>Children<span style=\"color: #808030;\">,<\/span> From<span style=\"color: #808030;\">.<\/span>GetOrAddComponent<span style=\"color: #808030;\">,<\/span> From<span style=\"color: #808030;\">.<\/span>Scene<span style=\"color: #808030;\">)]<\/span><\/span>\r\n<span style=\"color: #800000;\"><b>public<\/b><\/span> <span style=\"color: #800000;\"><b>class<\/b><\/span><span style=\"color: #000000;\"> Player <\/span><span style=\"color: #808030;\">:<\/span><span style=\"color: #000000;\"> MonoBehaviour<\/span><span style=\"color: #808030;\">&lt;<\/span><span style=\"color: #000000;\">Collider<\/span><span style=\"color: #808030;\">,<\/span><span style=\"color: #000000;\"> AnimatorController<\/span><span style=\"color: #808030;\">,<\/span><span style=\"color: #000000;\"> Camera<\/span><span style=\"color: #808030;\">&gt;<\/span><\/pre>\n<h2>Initializer Support<\/h2>\n<p>It is also possible to add this attribute to an Initializer class.<\/p>\n<h3>From Initializer to Client<\/h3>\n<p>In this case, by default, the Init arguments will be taken from the Initializer, and passed to its client&#8217;s Init function. In other words, no arguments will be gathered from the scene or prefab hierarchy in this case, and no arguments will be passed to the Initializer.<\/p>\n<h3>From Hierarchy to Initializer &amp; Client<\/h3>\n<p>If you specify from where arguments should be retrieved from, using any values other than From.Initializer, or From.Default, then the Init arguments <em>will<\/em> be located from the\u00a0 scene or prefab hierarchy, and passed to both the Initializer and its client.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Add this attribute to a component to have the arguments accepted by it be automatically gathered and passed to its Init function in Edit Mode, whenever any objects in the same scene or prefab that contains the component are modified. This attribute supports any components that implement IInitializable&lt;T&gt;, which includes all classes that derive from ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/init-in-edit-mode\/\" 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\/418"}],"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=418"}],"version-history":[{"count":5,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/418\/revisions"}],"predecessor-version":[{"id":615,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/418\/revisions\/615"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}