Where did that component go?
In larger Unity projects, components sometimes “get lost” – you refactor something and there’s an audio source still attached somewhere in your hierarchy and you simply cannot remember where.
You’re not the first one. A while ago I created a simple editor script to combat this issue as a response to a similar frustrated post on the Unity forums. Earlier this week I stumbled on the script again, picked it up and dusted it off a bit and decided to give it another chance to shine in some spotlight.
Use:
- Save the script as ComponentLister.cs.
- Place it in /Assets/Editor in your project.
- Launch the window from the Component menu – should be the last item titled “Component lister”.
- Click “Refresh” to list all components in your scene and the GameObjects to which they are attached.
- To investigate, click a GameObject name in the list and it will be set as the active selection in the hierarchy.
Codes:
public class ComponentLister : EditorWindow
{
private Hashtable sets = new Hashtable();
private Vector2 scrollPosition;
[ MenuItem( "Component/Component lister" ) ]
{
EditorWindow window = GetWindow( typeof( ComponentLister ) );
window.Show();
}
{
Object[] objects;
sets.Clear();
objects = FindObjectsOfType( typeof( Component ) );
foreach( Component component in objects )
{
if( !sets.ContainsKey( component.GetType() ) )
{
sets[ component.GetType() ] = new ArrayList();
}
( ( ArrayList )sets[ component.GetType() ] ).Add( component.gameObject );
}
}
{
GUILayout.BeginHorizontal( GUI.skin.GetStyle( "Box" ) );
GUILayout.Label( "Components in scene:" );
GUILayout.FlexibleSpace();
if( GUILayout.Button( "Refresh" ) )
{
UpdateList();
}
GUILayout.EndHorizontal();
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
foreach( System.Type type in sets.Keys )
{
GUILayout.Label( type.Name + ":" );
foreach( GameObject gameObject in ( ArrayList )sets[ type ] )
{
if( GUILayout.Button( gameObject.name ) )
{
Selection.activeObject = gameObject;
}
}
}
GUILayout.EndScrollView();
}
}
4 Comments
→
“Earlier this week I stumbled on the script again”
So you, er, lost it?
Nowai! I *filed* it – y’know?
I came across this gem of a script today – fantastic, thanks!
However I needed it to work with the iPhone Unity 1.5 environment where the “EditorWindow.GetWindow” method doesn’t exist. So I changed your Launch() method to:
—–
public static void Launch()
{
{
if (window)
DestroyImmediate(window);
window = new ComponentLister() ;
window.Show (true);
}
}
—-
and added the static class field
public static ComponentLister window;
It works with the iPhone environment now
Cheers!
i just love the support =) .
thank you.