Excess MonoBehaviours

Is there an easy way to search for a MonoBehaviour specifically? I have a bunch that have built up over the life of my project from scripts that have been deleted and those objects being copy+pasted to other areas. (When you delete a script it leaves an empty MonoBehaviour on the objects that your script was attached to)

I want to write an editor script at some point to delete them all but if I do a GetComponentsInChildren on MonoBehaviour, it’s going to return all of my custom scripts as well if I’m not mistaken.

Ideas would be appreciated.

I’m pretty sure that you can’t access empty scripts and delete them.

Guess I’ll have to go through by hand then, thanks.

Missing scripts show up as null when you fetch all of a game object’s components:

Transform target = Selection.activeTransform;

foreach (Component c in target.GetComponents<Component>()) {
	if (c == null) {
		Debug.Log("Missing script");
	}
}

A non-null component can be removed with the DestroyImmediate() function, but I was unable to figure out a way to remove a null component. This could be used to discover which game objects have missing scripts attached, if that would save any time.

Finding where they all are could help, thanks Matthew Miner.