{"id":82,"date":"2021-10-24T07:54:14","date_gmt":"2021-10-24T07:54:14","guid":{"rendered":"https:\/\/docs.sisus.co\/inity\/?p=82"},"modified":"2025-07-14T19:40:57","modified_gmt":"2025-07-14T19:40:57","slug":"gameobject-t","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/initializers\/gameobject-t\/","title":{"rendered":"4. new GameObject&lt;T&#8230;&gt;"},"content":{"rendered":"<p>The generic GameObject structs are builders that can be used to initialize a GameObject and upto three components in a single line of code.<\/p>\n<p>If you want a GameObject with a single component to be build, initialize a new instance of the GameObject&lt;T&gt; struct and specify the type of the component you want attached to the GameObject as the generic argument.<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\"><span style=\"color: #800000;\"><b>new<\/b><\/span> GameObject<span style=\"color: #808030;\">&lt;<\/span>Camera<span style=\"color: #808030;\">&gt;()<\/span><span style=\"color: #800080;\">;<\/span><\/span><\/pre>\n<p>Optionally you can customize the state of the created GameObject, such as its name, parent, active state, position, rotation and scale.<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\"><span style=\"color: #800000;\"><b>new<\/b><\/span> GameObject<span style=\"color: #808030;\">&lt;<\/span>Camera<span style=\"color: #808030;\">&gt;(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #800000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">Camera<\/span><\/span><\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #808030;\">, <\/span><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">transform<\/span><\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/span><\/pre>\n<p>To finalize the building process you also need to call Init() on the GameObject&lt;T&gt; instance or cast it into a GameObject or the type of the created component.<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">Camera camera1 <\/span><\/span><span style=\"color: #808030;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">=<\/span><\/span><\/span> <span style=\"color: #800000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\"><b>new<\/b><\/span><\/span><\/span><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\"> GameObject<\/span><\/span><span style=\"color: #808030;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">&lt;<\/span><\/span><\/span><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">Camera<\/span><\/span><span style=\"color: #808030;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">&gt;(<\/span><\/span><\/span><span style=\"color: #800000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">\"<\/span><\/span><\/span><span style=\"color: #800000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">Camera <\/span><\/span><\/span><span style=\"color: #800000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">1<\/span><\/span><\/span><span style=\"color: #800000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">\"<\/span><\/span><\/span><span style=\"color: #808030;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">).<\/span><\/span><\/span><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">Init<\/span><\/span><span style=\"color: #808030;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">()<\/span><\/span><\/span><span style=\"color: #800080;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">;<\/span><\/span><\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">Camera camera2 <\/span><\/span><span style=\"color: #808030;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">=<\/span><\/span><\/span> <span style=\"color: #800000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\"><b>new<\/b><\/span><\/span><\/span><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\"> GameObject<\/span><\/span><span style=\"color: #808030;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">&lt;<\/span><\/span><\/span><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">Camera<\/span><\/span><span style=\"color: #808030;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">&gt;(<\/span><\/span><\/span><span style=\"color: #800000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">\"<\/span><\/span><\/span><span style=\"color: #800000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">Camera <\/span><\/span><\/span><span style=\"color: #800000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">2<\/span><\/span><\/span><span style=\"color: #800000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">\"<\/span><\/span><\/span><span style=\"color: #808030;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">)<\/span><\/span><\/span><span style=\"color: #800080;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">;<\/span><\/span><\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">GameObject<\/span><\/span> camera3 <span style=\"color: #808030;\">=<\/span> <span style=\"color: #800000;\"><b>new<\/b><\/span> GameObject<span style=\"color: #808030;\">&lt;<\/span>Camera<span style=\"color: #808030;\">&gt;(<\/span><span style=\"color: #800000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">\"<\/span><\/span><\/span><span style=\"color: #800000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">Camera <\/span><\/span><\/span><span style=\"color: #800000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">3<\/span><\/span><\/span><span style=\"color: #800000;\"><span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">\"<\/span><\/span><\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/span><\/pre>\n<p>If one of components being initialized derives from a <a href=\"https:\/\/docs.sisus.co\/inity\/features\/monobehaviour-t\/\">MonoBehaviour&lt;T&#8230;&gt;<\/a> base class or implements an <a href=\"https:\/\/docs.sisus.co\/inity\/features\/interfaces\/\">IArgs&lt;T\u2026&gt;<\/a> interface, then it is also possible to pass dependencies to the component as arguments of the Init function.<\/p>\n<p>For example, let\u2019s say we had we had the following IInputManager interface and class that implements the interface:<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\"><span style=\"color: #800000;\"><b>public<\/b><\/span> <span style=\"color: #800000;\"><b>interface<\/b><\/span> IInputManager<\/span>\r\n<span style=\"color: #800080;\">{<\/span>\r\n<span style=\"color: #000000;\">   Vector2 Input <span style=\"color: #800080;\">{<\/span> <span style=\"color: #800000;\"><b>get<\/b><\/span><span style=\"color: #800080;\">;<\/span> <span style=\"color: #800080;\">}<\/span><\/span>\r\n<span style=\"color: #800080;\">}<\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #800000;\"><b>public<\/b><\/span> <span style=\"color: #800000;\"><b>class<\/b><\/span> InputManager <span style=\"color: #808030;\">:<\/span> IInputManager<\/span>\r\n<span style=\"color: #800080;\">{<\/span>\r\n<span style=\"color: #000000;\">   <span style=\"color: #800000;\"><b>public<\/b><\/span> Vector2 Input<\/span>\r\n<span style=\"color: #000000;\">   <span style=\"color: #800080;\">{<\/span><\/span>\r\n<span style=\"color: #000000;\">      <span style=\"color: #800000;\"><b>get<\/b><\/span> <span style=\"color: #808030;\">=&gt;<\/span> <span style=\"color: #800000;\"><b>new<\/b><\/span> Vector2<span style=\"color: #808030;\">(<\/span>Input<span style=\"color: #808030;\">.<\/span>GetAxis<span style=\"color: #808030;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #800000;\">X<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #808030;\">),<\/span> Input<span style=\"color: #808030;\">.<\/span>GetAxis<span style=\"color: #808030;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #800000;\">Y<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #808030;\">))<\/span><span style=\"color: #800080;\">;<\/span><\/span>\r\n<span style=\"color: #000000;\">   <span style=\"color: #800080;\">}<\/span><\/span>\r\n<span style=\"color: #800080;\">}<\/span><\/pre>\n<p>And let\u2019s say we had the following Player class that can be initialized with IInputManager and Collider arguments:<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\"><span style=\"color: #800000;\"><b>public<\/b><\/span> <span style=\"color: #800000;\"><b>sealed<\/b><\/span> <span style=\"color: #800000;\"><b>class<\/b><\/span> Player <span style=\"color: #808030;\">:<\/span> MonoBehaviour<span style=\"color: #808030;\">&lt;<\/span>IInputManager<span style=\"color: #808030;\">,<\/span> Collider<span style=\"color: #808030;\">&gt;<\/span><\/span>\r\n<span style=\"color: #800080;\">{<\/span>\r\n<span style=\"color: #000000;\">   <span style=\"color: #800000;\"><b>public<\/b><\/span> IInputManager InputManager <span style=\"color: #800080;\">{<\/span> <span style=\"color: #800000;\"><b>get<\/b><\/span><span style=\"color: #800080;\">;<\/span> <span style=\"color: #800000;\"><b>private<\/b><\/span> <span style=\"color: #800000;\"><b>set<\/b><\/span><span style=\"color: #800080;\">;<\/span> <span style=\"color: #800080;\">}<\/span><\/span>\r\n<span style=\"color: #000000;\">   <span style=\"color: #800000;\"><b>public<\/b><\/span> Collider Collider <span style=\"color: #800080;\">{<\/span> <span style=\"color: #800000;\"><b>get<\/b><\/span><span style=\"color: #800080;\">;<\/span> <span style=\"color: #800000;\"><b>private<\/b><\/span> <span style=\"color: #800000;\"><b>set<\/b><\/span><span style=\"color: #800080;\">;}<\/span><\/span>\r\n\r\n<span style=\"color: #000000;\">   <span style=\"color: #800000;\"><b>protected<\/b><\/span> <span style=\"color: #800000;\"><b>override<\/b><\/span> Init<span style=\"color: #808030;\">(<\/span>IInputManager inputManager<span style=\"color: #808030;\">,<\/span> Collider collider<span style=\"color: #808030;\">)<\/span><\/span>\r\n<span style=\"color: #000000;\">   <span style=\"color: #800080;\">{<\/span><\/span>\r\n<span style=\"color: #000000;\">      InputManager <span style=\"color: #808030;\">=<\/span>  inputManager<span style=\"color: #800080;\">;<\/span><\/span>\r\n<span style=\"color: #000000;\">      Collider <span style=\"color: #808030;\">=<\/span>  collider<span style=\"color: #800080;\">;<\/span><\/span>\r\n<span style=\"color: #000000;\">   <span style=\"color: #800080;\">}<\/span><\/span>\r\n<span style=\"color: #800080;\">}<\/span><\/pre>\n<p>Then we could create a new instance of the Player component and initialize it using the following syntax:<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\">IInputManager inputManager <span style=\"color: #808030;\">=<\/span> <span style=\"color: #800000;\"><b>new<\/b><\/span> InputManager<span style=\"color: #808030;\">()<\/span><span style=\"color: #800080;\">;<\/span><\/span>\r\n\r\n<span style=\"color: #000000;\">Player player <span style=\"color: #808030;\">=<\/span> <span style=\"color: #800000;\"><b>new<\/b><\/span> GameObject<span style=\"color: #808030;\">&lt;<\/span>Player<span style=\"color: #808030;\">,<\/span> BoxCollider<span style=\"color: #808030;\">&gt;(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #800000;\">Player\"<\/span><span style=\"color: #808030;\">)<\/span><\/span>\r\n<span style=\"color: #000000;\">                                <span style=\"color: #808030;\">.<\/span>Init1<span style=\"color: #808030;\">(<\/span>inputManager<span style=\"color: #808030;\">,<\/span> Second<span style=\"color: #808030;\">.<\/span>Component<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/span><\/pre>\n<p>The Init1 function tells the builder to initialize the first added component, i.e. the Player component, with the provided arguments.<\/p>\n<p>The Second.Component token instructs the builder to pass the second added component, i.e. the BoxCollider, as the second initialization argument.<\/p>\n<p>In this case you can skip calling the Init2 function of the builder, since only the first added component accepts initialization arguments.<\/p>\n<p>If the generic arguments were reversed the syntax to build the same GameObject would look like this instead:<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\">IInputManager inputManager <span style=\"color: #808030;\">=<\/span> <span style=\"color: #800000;\"><b>new<\/b><\/span> InputManager<span style=\"color: #808030;\">()<\/span><span style=\"color: #800080;\">;<\/span><\/span>\r\n\r\n<span style=\"color: #000000;\">Player player <span style=\"color: #808030;\">=<\/span> <span style=\"color: #800000;\"><b>new<\/b><\/span> GameObject<span style=\"color: #808030;\">&lt;<\/span>BoxCollider, Player<span style=\"color: #808030;\">&gt;(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #800000;\">Player\"<\/span><span style=\"color: #808030;\">)<\/span><\/span>\r\n<span style=\"color: #000000;\">                                <span style=\"color: #808030;\">.<\/span>Init<span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">1<\/span><\/span><span style=\"color: #808030;\">()<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #808030;\">                                .<\/span>Init2<span style=\"color: #808030;\">(<\/span>inputManager<span style=\"color: #808030;\">,<\/span> <span style=\"font-family: Liberation Mono, monospace;\"><span style=\"font-size: small;\">First<\/span><\/span><span style=\"color: #808030;\">.<\/span>Component<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/span>\r\n<\/pre>\n<p>You always have to invoke all the Init functions in the same order as the component types have been defined in the generic arguments of the GameObject builder. Because of this you now also have to call the Init1 function to initialize the BoxCollider component, even though it does not accept any arguments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The generic GameObject structs are builders that can be used to initialize a GameObject and upto three components in a single line of code. If you want a GameObject with a single component to be build, initialize a new instance of the GameObject&lt;T&gt; struct and specify the type of the component you want attached to ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/initializers\/gameobject-t\/\" 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\/82"}],"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=82"}],"version-history":[{"count":9,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"predecessor-version":[{"id":611,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/82\/revisions\/611"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}