{"id":2155,"date":"2018-01-24T19:33:53","date_gmt":"2018-01-24T10:33:53","guid":{"rendered":"https:\/\/www.codedojo.com\/?p=2155"},"modified":"2020-02-27T08:20:53","modified_gmt":"2020-02-26T23:20:53","slug":"unity-snippet-finding-a-game-object-by-name-even-inactive-or-disabled-ones","status":"publish","type":"post","link":"https:\/\/www.codedojo.com\/?p=2155","title":{"rendered":"Unity snippet: Finding a GameObject by name, even inactive or disabled ones"},"content":{"rendered":"<p>I use <strong>GameObject.Find()<\/strong>\u00a0in Unity for things like enabling or fading in\/out a menu or to grab an object reference via code to store for later.\u00a0 \u00a0(I usually prefer doing things in code rather than drag and dropping references using the Unity Editor when I can)<\/p>\n<p>A problem is <strong>GameObject.Find()<\/strong> won&#8217;t locate inactive gameobjects which causes me problems because I tend to have inactive object trees in a scene that are just turned on\/off when they are being used, like a GUI menu for example.\u00a0 It&#8217;s just kind of my programming style to do things that way.<\/p>\n<p>I couldn&#8217;t find a clean full snippet for this online that used\u00a0<strong>scene.GetRootGameObjects<\/strong>, so figured I&#8217;d post one.<\/p>\n<p>Cut and paste this to <strong>MyUtils.cs<\/strong>\u00a0or your own utils class:<\/p>\n<pre>using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\nusing UnityEngine.SceneManagement;\r\n\r\npublic class MyUtils \r\n{\r\n\r\n    \/\/hideously slow as it iterates all objects, so don't overuse!\r\n    public static GameObject FindInChildrenIncludingInactive(GameObject go, string name)\r\n    {\r\n\r\n        for (int i=0; i &lt; go.transform.childCount; i++)\r\n        {\r\n            if (go.transform.GetChild(i).gameObject.name == name) return go.transform.GetChild(i).gameObject;\r\n            GameObject found = FindInChildrenIncludingInactive(go.transform.GetChild(i).gameObject, name);\r\n            if (found != null) return found;\r\n        }\r\n\r\n        return null;  \/\/couldn't find crap\r\n    }\r\n    \r\n    \/\/hideously slow as it iterates all objects, so don't overuse!\r\n    public static GameObject FindIncludingInactive(string name)\r\n    {\r\n        Scene scene = SceneManager.GetActiveScene();\r\n        if (!scene.isLoaded)\r\n        {\r\n            \/\/no scene loaded\r\n            return null;\r\n        }\r\n\r\n        var game_objects = new List();\r\n        scene.GetRootGameObjects(game_objects);\r\n\r\n        foreach (GameObject obj in game_objects)\r\n        {\r\n            if (obj.transform.name == name) return obj;\r\n\r\n            GameObject found = FindInChildrenIncludingInactive(obj, name);\r\n            if (found) return found;\r\n         }\r\n\r\n        return null;\r\n    }\r\n\r\n}\r\n\r\n<\/pre>\n<p>And use it from anywhere like:<\/p>\n<p><strong>GameObject obj = MyUtil.FindIncludingInactive(&#8220;MyMenuName&#8221;);<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I use GameObject.Find()\u00a0in Unity for things like enabling or fading in\/out a menu or to grab an object reference via code to store for later.\u00a0 \u00a0(I usually prefer doing things in code rather than drag and dropping references using the Unity Editor when I can) A problem is GameObject.Find() won&#8217;t locate inactive gameobjects which causes [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,21],"tags":[],"class_list":["post-2155","post","type-post","status-publish","format-standard","hentry","category-tech-tips","category-unity"],"_links":{"self":[{"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/posts\/2155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2155"}],"version-history":[{"count":10,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/posts\/2155\/revisions"}],"predecessor-version":[{"id":2566,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/posts\/2155\/revisions\/2566"}],"wp:attachment":[{"href":"https:\/\/www.codedojo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}