{"id":2323,"date":"2020-10-13T11:02:28","date_gmt":"2020-10-13T03:02:28","guid":{"rendered":"https:\/\/www.clonefactor.com\/wordpress\/?p=2323"},"modified":"2020-10-13T11:06:57","modified_gmt":"2020-10-13T03:06:57","slug":"create-gameobject-return-null-in-editor-mode","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/unity3d\/2323\/","title":{"rendered":"New GameObject return null, in Editor mode."},"content":{"rendered":"\n<p>It happen in Unity3D Editor mode.<\/p>\n<p>When I want to auto create a folder like structure, and error show in console.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void Reset()\n{\nGameObject go = new GameObject(\"Sub-Folder\");\ngo.transform.SetParent(transform); \/\/ Null reference \"go == null\"\n}<\/pre>\n<p>the code above will fail. due to &#8220;go&#8221; value is null.<br \/>so the solution is simple.<br \/>DON&#8217;T process &#8220;new GameObject(blah)&#8221; within Editor session.<br \/>I prefer using &#8220;Invoke&#8221; hack in this case.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void Reset()\n{\nInvoke(nameof(Editor_Init), 0); \/\/ try not to use \"string\" type here, <br \/>\/\/ instead we can use nameof() to label which function call we using.<br \/>\/\/ process in next ZERO second is a hack, to run the same thing without error.\n}\n\nvoid Editor_Init()\n{\nGameObject go = new GameObject(\"Sub-Folder\");\ngo.transform.SetParent(transform); \/\/ will work like this. ya~~~\n}<\/pre>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It happen in Unity3D Editor mode. When I want to a &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[114,115],"class_list":["post-2323","post","type-post","status-publish","format-standard","hentry","category-unity3d","tag-hack","tag-hotfix"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2323"}],"version-history":[{"count":3,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2323\/revisions"}],"predecessor-version":[{"id":2326,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2323\/revisions\/2326"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}