- Home /
Quickly isolate GameObjects in Hierarchy with scripts in them?
In the Unity IDE: Is there a way to quickly find out which GameObjects in Hierarchy have scripts attached? Is there a way to find out which scripts are attached to which objects?
Get all the objects in the scene in an array, and do a GetComponent with an each statement, and then /win... Sorry, I don't know... But this is plausible, but very bad =P.
i guess that could be a handy editor script, outputting to Debug.Log... but i was hoping for more integration with the IDE, so i could easily click to inspect each one with scripts in them
Answer by chief1234 · Aug 23, 2011 at 08:14 PM
Not sure when this feature was added, but now there is a built-in solution.
Simply type the FULL NAME of the component you're looking for into the hierarchy search box and it will highlight all objects with that component.
Example: typing "anim" will only come up with gameObjects with "anim" in the name.
typing "animation" will come up with all gameObjects with "animation" components attached.
For some reason, "animation" doesn't work anymore (I'm guessing it hasn't worked since they added $$anonymous$$ecanim).
One small correction - this is also case-sensitive now as well (it wasn't always). And yes, you're right - animation, or Animation, does not seem to work anymore.
Answer by yoyo · Dec 30, 2010 at 06:15 PM
Add this script to your Assets\Editor folder and you will get a menu item that dumps information about the scene to a text file, including which components (scripts) are attached to which objects.
It basically implements Justin's suggestion.
You could modify the script to be more fully integrated with the IDE. Reverse lookup would be a cool feature -- i.e. "select all game objects using this script." Hmm, useful enough that maybe I'll go implement it ...
OK, so I got a little carried away, but see http://zeroandone.ca/unity for a package that implements this and other features. Here's a screenshot (click here for full size image) ...
You can grab the package and look at the source code, or just use it. The implementation does roughly this ...
- select your script in the Project view (identified as Selection.objects from an editor script)
- get this as a MonoScript object representing the script asset
- use MonoScript.GetClass to determine the component type (returns null for scripts that don't implement MonoBehaviour)
- search all the root objects of the scene (transform.parent == null) and their descendants for use of the component type, with GameObject.GetComponentsInChildren
- select the found objects by setting Selection.objects
- Debug.Log as well, with a game object context parameter for handy highlighting
stay tuned, should have something tomorrow ... (famous last words)
See edited answer -- follow the zeroandone link to see what I came up with. Heck, I'll edit my answer again and add a screenshot ...
brilliant, thanks so much. This should be standard in Unity its used so much.
Cool, thanks, I hope it's useful. I need to go back to that script and make it better at digging into arrays and lists, which I now know how to do.
Your answer
Follow this Question
Related Questions
Unity IDE: Finding out object type of arbitrary item in Hierarchy 3 Answers
C# Scripts don't attach to game objects, what happened? 2 Answers
True scale of a visible GameObject with many childs 1 Answer
How to add an asset to a script-enabled public game object? 1 Answer
Deleting GameObjects 1 Answer