{"id":326,"date":"2022-09-17T21:38:05","date_gmt":"2022-09-17T21:38:05","guid":{"rendered":"https:\/\/docs.sisus.co\/init-args\/?p=326"},"modified":"2026-02-03T06:00:50","modified_gmt":"2026-02-03T06:00:50","slug":"initializing-components-in-code","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/getting-started\/initializing-components-in-code\/","title":{"rendered":"3. Initializing Components in Code"},"content":{"rendered":"<h1>AddComponent with Arguments<\/h1>\n<p>A new component that derives from <a href=\"https:\/\/docs.sisus.co\/init-args\/clients\/monobehaviour-t\/\">MonoBehaviour&lt;T&#8230;&gt;<\/a> or implements <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/iinitializable\/\">IInitializable&lt;T&#8230;&gt;<\/a> can be attached to GameObject and initialized with arguments using the <a href=\"https:\/\/docs.sisus.co\/init-args-reference\/class_sisus_1_1_init_1_1_add_component_extensions.html\"><em>AddComponent&lt;TComponent, T&#8230;&gt; extension methods<\/em><\/a> in the <em>Sisus.Init<\/em> namespace.<\/p>\n<p>The methods work just like the <em><a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/GameObject.AddComponent.html\">built-in AddComponent&lt;T&gt; methods<\/a><\/em>, except you also list the types of the component&#8217;s Init parameters as generic type arguments, and then pass in the Init arguments when calling the method.<\/p>\n<pre>using Sisus.Init;\r\nusing UnityEngine;\r\n\r\npublic static class GameManager\r\n{\r\n    public static IInputManager InputManager { get; } = new InputManager();\r\n\r\n    public static Player CreatePlayer()\r\n    {\r\n        var gameObject = new GameObject(\"Player\");\r\n        return gameObject.AddComponent&lt;Player, IInputManager, Camera&gt;(InputManager, Camera.main);\r\n    }\r\n}\r\n\r\npublic class Player : MonoBehaviour&lt;IInputManager, Camera&gt;\r\n{\r\n    IInputManager inputManager;\r\n    Camera camera;\r\n\r\n    protected override void Init(IInputManager inputManager, Camera camera)\r\n    {\r\n        this.inputManager = inputManager;\r\n        this.camera = camera;\r\n    }\r\n}<\/pre>\n<p>There are also overloads available which don&#8217;t return the created component as a return value, but instead assign it into an <strong>out<\/strong> argument. This can help the compiler be able to infer the types of all the generic type arguments of the method from the types of the variables that are passed to it, so you don&#8217;t have to explicitly specify all of them.<\/p>\n<pre>public static Player CreatePlayer()\r\n{\r\n    var gameObject = new GameObject(\"Player\");\r\n    gameObject.AddComponent(out Player player, InputManager, Camera.main);\r\n    return player;\r\n}<\/pre>\n<h1>Instantiate with Arguments<\/h1>\n<p>A component that derives from <a href=\"https:\/\/docs.sisus.co\/init-args\/clients\/monobehaviour-t\/\">MonoBehaviour&lt;T&#8230;&gt;<\/a> or implements <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/iinitializable\/\">IInitializable&lt;T&#8230;&gt;<\/a>\u00a0can be instantiated by cloning a GameObject, and initialized with arguments using the <a href=\"https:\/\/docs.sisus.co\/init-args-reference\/class_sisus_1_1_init_1_1_instantiate_extensions.html\"><em>Instantiate&lt;TObject, T&#8230;&gt; extension methods<\/em><\/a>\u00a0in the <em>Sisus.Init<\/em> namespace.<\/p>\n<p>The methods work just like the<em> <a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/GameObject.AddComponent.html\">built-in AddComponent&lt;T&gt; methods<\/a><\/em>, except you also pass in the Init arguments when calling the method.<\/p>\n<pre>using Sisus.Init;\r\nusing UnityEngine;\r\n\r\npublic static class GameManager\r\n{\r\n    public static IInputManager InputManager { get; } = new InputManager();\r\n\r\n    public static Player CreatePlayer(Player prefab)\r\n    {\r\n        return prefab.Instantiate(InputManager, Camera.main);\r\n    }\r\n}<\/pre>\n<p>You typically don&#8217;t need to explicitly specify the generic type arguments of the method, as the compiler can usually infer them from the types of the variables that are passed to it.<\/p>\n<p>If the types of some of the variables passed to the Instantiate method don&#8217;t match the types of the corresponding Init parameters exactly, you might need to <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/operators\/type-testing-and-cast#cast-expression\">cast<\/a> them to the exact types to help out the compiler:<\/p>\n<pre>public static Player CreatePlayer(Player prefab)\r\n{\r\n    var inputManager = new InputManager();\r\n    return prefab.Instantiate((IInputManager)inputManager, Camera.main);\r\n}<\/pre>\n<h1>new GameObject with Arguments<\/h1>\n<p>The <a href=\"https:\/\/docs.sisus.co\/init-args-reference\/struct_sisus_1_1_init_1_1_game_object.html\"><em>new GameObject&lt;TComponent&#8230;&gt;<\/em><\/a> structure can be used to create a new GameObject, attach one or more components to it, and initialize zero or more of those components with arguments &#8211; all in a single line of code.<\/p>\n<p>When attaching a single component to the created GameObject, you can pass Init arguments to the created component using the Init method.<\/p>\n<pre>using Sisus.Init;\r\nusing UnityEngine;\r\n\r\npublic static class GameManager\r\n{\r\n    public static IInputManager InputManager { get; } = new InputManager();\r\n\r\n    public static Player CreatePlayerWithMainCamera()\r\n    {\r\n        return new GameObject&lt;Player&gt;(\"Player\").Init(InputManager, Camera.main);\r\n    }\r\n}<\/pre>\n<p>When attaching a multiple components to the created GameObject, you can pass Init arguments to the first component using the Init1 method, to the second one usings Init2 etc.<\/p>\n<pre>public static Player CreatePlayerWithNewCamera()\r\n{\r\n    return new GameObject&lt;Player, Camera&gt;(\"Player\").Init1(InputManager, Second.Component);\r\n}<\/pre>\n<p>If a component doesn&#8217;t require any Init arguments, you can skip over it by executing its Init method without passing any arguments to it.<\/p>\n<pre>public static Player CreatePlayerWithNewCamera()\r\n{\r\n    return new GameObject&lt;Camera, Player&gt;(\"Player\")\r\n        .Init1()\r\n        .Init2(InputManager, First.Component);\r\n}<\/pre>\n<p>new GameObject&lt;T&#8230;&gt; can be implicitly converted into any of attached components, into a tuple of all the attached components, or into a GameObject:<\/p>\n<pre>A a = new GameObject&lt;A, B, C&gt;();\r\nB b = new GameObject&lt;A, B, C&gt;();\r\nC C = new GameObject&lt;A, B, C&gt;();\r\n(A a, B b, C c) components = new GameObject&lt;A, B, C&gt;();\r\nGameObject gameObject = new GameObject&lt;A, B, C&gt;();<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>AddComponent with Arguments A new component that derives from MonoBehaviour&lt;T&#8230;&gt; or implements IInitializable&lt;T&#8230;&gt; can be attached to GameObject and initialized with arguments using the AddComponent&lt;TComponent, T&#8230;&gt; extension methods in the Sisus.Init namespace. The methods work just like the built-in AddComponent&lt;T&gt; methods, except you also list the types of the component&#8217;s Init parameters as generic type ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/getting-started\/initializing-components-in-code\/\" 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":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/326"}],"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=326"}],"version-history":[{"count":13,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/326\/revisions"}],"predecessor-version":[{"id":1093,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/326\/revisions\/1093"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}