- Home /
How to know where a particular script is being used?
Hi I created a project a while back. Now I'm opening it and I find that I've forgotten most of what I've done. I'm trying to remove a script from the game but don't remember which object/prefab I attached it to. Is there a way to see where a particular script is being used at? something like "select dependencies" but the opposite.
Answer by jonas-echterhoff · Dec 16, 2010 at 03:28 PM
If you know which scene it is in, just use the search field in the Hierarchy view (in Unity 3). Be sure to type the full script name, or you won't see any results.
Aah no wonder I cant find it, I didnt prefab the object. Thanks :)
But if you want to know whether it is being used anywhere in your project?
It won't find if it is on a prefab that is instanciated at runtime in your scene tough.
Answer by ynedelin · Sep 05, 2014 at 04:52 AM
In the project view right click on the script and choose "Find References In Scene".
Answer by ThePilgrim · Jul 21, 2016 at 09:25 PM
public class FindPrefabsWithComponent : MonoBehaviour {
public GameObject[] prefabs;
void Start()
{
FindPrefabsWithComponentType<Rigidbody>();
}
private void FindPrefabsWithComponentType<T>() where T : Component
{
print("Searching for components...");
foreach (var prefab in prefabs)
{
T foundComponent = prefab.GetComponentInChildren<T>();
if (foundComponent != null)
{
print("Found component in " + foundComponent.name);
}
}
print("search complete");
}
}
If you want to find all prefabs that the script is attached to, even ones that are not in any of your scenes, you can follow these instructions:
Create the FindPrefabsWithComponent c# script above, but replace "Rigidbody" with whatever script you are wanting to find.
Create a new game object in your scene and attach the FindPrefabsWithComponent script.
Click the small lock icon in the top right of the inspector window to lock the inspector view onto your new game object.
In your project, search for all prefabs with "t:Prefab" or by using the filter button immediately to the right of the search box in the project window.
Select all of the prefabs in the project window (Ctrl-A on windows) and drag them onto "Prefabs" in the FindPrefabsWithComponent component in the inspector window.
Run the game. It will print out the name of the game object that the script is attached to.