[Editor] Find all objects with a particular script in the scene

Hello there,

I am trying to write a editor script which selects all the game objects that has a particular script attached to. So far, I found a way to search for the scripts in the scene and show the list of monobehaviours,using reflection and also selecting works properly. The below is the script used for that,

   using UnityEngine;
   using UnityEditor;
   using System.Collections;
   using System.Collections.Generic;
   using System.Reflection;   
   using System;


    public class ScriptFinder : EditorWindow
    {
    	static ScriptFinder window;
    	
    	static string scriptValue="";
    	static string oldScriptValue;
    	
    	static List<Type> components;
    	static List<string> componentNames;
    	static List<GameObject> sceneObjects;
    	static int selectedIndex=0;
    	static int prevIndex=0;
    	static Vector2 scrollValue=Vector2.zero;
    	
    	//public 
    	// Use this for initialization
    	[MenuItem("EditorUtility/Script Finder")]
    	static void OpenScriptFinder()
    	{
    		//EditorUtility.FocusProjectWindow();		
    		window = (ScriptFinder) EditorWindow.GetWindow (typeof (ScriptFinder));	
    		
    		/*
    		Assembly _asm = Assembly.GetAssembly(typeof(AnimationComponent));
    		
    		AssemblyName _asmName =  _asm.GetName();
    		
    		Debug.Log(_asmName.FullName);
    		
    		*/
    		
    		Assembly _assembly = Assembly.Load("Assembly-CSharp");
    		
    		components = new List<Type>();
    		componentNames = new List<string>();
    		
    		foreach ( Type type in _assembly.GetTypes())
    		{
    			if(type.IsClass)
    			{
    				if(type.BaseType.FullName.Contains("MonoBehaviour")){
    					components.Add(type);
    					componentNames.Add(type.Name);
    //					Debug.Log(type.Name);
    				}
    				else
    				{
    					if(!type.BaseType.FullName.Contains("System"))
    					{
    						Type  _type = type.BaseType;						
    						components.Add(_type);
    						componentNames.Add(type.Name);
    //						Debug.Log(type.Name);
    					}
    				}
    			}
    		}
    		
    		
    		prevIndex = selectedIndex;
    		SelectScript(components[selectedIndex]);
    		
    	}
    	
    	static void SelectScript(Type _type)
    	{
    		
    		Debug.Log("Selected Type: "+_type);
    		
    		sceneObjects = new List<GameObject>();
    //		Debug.Log(GameObject.FindSceneObjectsOfType(typeof(GameObject)).Length);
    		foreach(UnityEngine.Object _obj in GameObject.FindSceneObjectsOfType(_type))
    		{
    			Debug.Log(_obj.name);			
    			
    			sceneObjects.Add(GameObject.Find(_obj.name));
    		}
    		
    		Selection.objects = sceneObjects.ToArray();
    	}
    	
    	
    	void OnGUI()
    	{
    		selectedIndex = EditorGUILayout.Popup(selectedIndex,componentNames.ToArray());
    		
    		if(selectedIndex!=prevIndex)
    		{
    			SelectScript(components[selectedIndex]);
    		}
    		
    		scrollValue=EditorGUILayout.BeginScrollView(scrollValue);
    		
    		foreach(GameObject _obj in sceneObjects)
    		{
    		
    			if(GUILayout.Button(_obj.name,GUIStyle.none))
    			{
    				Selection.activeObject = _obj;
    				EditorGUIUtility.PingObject(_obj);
    			}
    			
    		}
    		
    		EditorGUILayout.EndScrollView();
    		
    		prevIndex = selectedIndex;
    	}
    }

Create a script called ScriptFinder and copy the code, it works perfectly fine. But the problem is that, it is working only for C# scripts, is there a way to find the javascript classes using this method or any other approach?

Please help!!

Um - I hate to be the bearer of bad news - but it already does that :slight_smile:

Type the script name into the search box on the hierarchy view. (You can also prefix it with t: to ensure it doesn’t select any objects with that name).

BTW you can find all scripts by doing:

  Resources.FindObjectsOfTypeAll(typeof(MonoScript))

The MonoScript will give you the class defined by the script and excluding ones without hideflags will ignore the system ones you don’t want.

You can check all assemblies by doing

  AppDomain.CurrentDomain.GetAssemblies()