Find game object in hierarchy by tag! Editor extension

Hello guys!

Just wanted to share a very small tool I created for the Unity editor, it allows you to find game objects in the hierarchy by tag, very simple yet effective.

Hope you guys find it useful.

ezgif-3-e34cb91f06

Here's the code. For it to work you need to put it inside a folder named 'Editor'.
using UnityEngine;
using UnityEditor;

namespace Yee.Tools
{
    public class FinderByTag : EditorWindow
    {
        [MenuItem("Find/Find by Tag")]
        static void Init()
        {
            FinderByTag window = ScriptableObject.CreateInstance<FinderByTag>();
            float height = UnityEditorInternal.InternalEditorUtility.tags.Length * 21;
            window.position = new Rect(Screen.width / 2, Screen.height / 2 - height / 2, 250, height);
            window.ShowPopup();
        }

        void OnGUI()
        {
            foreach (string tag in UnityEditorInternal.InternalEditorUtility.tags)
            {
                if (GUILayout.Button(tag))
                {
                    GameObject objectWithTag = GameObject.FindGameObjectWithTag(tag);

                    if (objectWithTag != null)
                    {
                        Debug.Log(objectWithTag);
                        Selection.activeGameObject = objectWithTag;
                    }
                    else { Debug.Log($"No Game Objects with the tag: {tag}, were found in the hierarchy"); }

                    Close();
                }
            }
        }
    }
}

There are some things that can be done to improve this, like being able to move the window around and have a close button instead so you can search for any game object without the window closing. I’ll make those updates later, but as it is, the tool works fine, feel free to use it.

1 Like

Made a quick improvement to select all objects with the same Tag. Easier to grab them and place them in an array.

Changes:

  1. Replaced GameObject.FindGameObjectWithTag with GameObject.FindGameObjectsWithTag to find all objects with the selected tag.
  2. Assigned all found objects to Selection.objects so that all of them are selected in the editor.
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;

namespace Yee.Tools
{
    public class FinderByTag : EditorWindow
    {
        [MenuItem("Find/Find by Tag")]
        static void Init()
        {
            FinderByTag window = ScriptableObject.CreateInstance<FinderByTag>();
            float height = UnityEditorInternal.InternalEditorUtility.tags.Length * 21;
            window.position = new Rect(Screen.width / 2, Screen.height / 2 - height / 2, 250, height);
            window.ShowPopup();
        }

        void OnGUI()
        {
            foreach (string tag in UnityEditorInternal.InternalEditorUtility.tags)
            {
                if (GUILayout.Button(tag))
                {
                    GameObject[] objectsWithTag = GameObject.FindGameObjectsWithTag(tag);

                    if (objectsWithTag.Length > 0)
                    {
                        Debug.Log($"{objectsWithTag.Length} objects with tag: {tag} found.");
                        Selection.objects = objectsWithTag;
                    }
                    else
                    {
                        Debug.Log($"No Game Objects with the tag: {tag} were found in the hierarchy");
                    }

                    Close();
                }
            }
        }
    }
}
#endif
1 Like