Easily Remove Missing Components from GameObjects

Published

When importing an all package, it’s not unusual to get an error about missing components. In some cases this can be problematic as there are many of them, and deleting all of them by hand is, quite frankly, a chore.

I made a small script to aid in this task. It takes as input an array of GameObjects and deletes all missing components.

The trick here is that the components are considered as “null” by Unity so you cannot use Destroy or DestroyImmediate, but instead GameObjectUtility.RemoveMonoBehavioursWithMissingScript from the Unity.Editor library.

IThe code for this missing component cleaner is available on my Unity Utils repo on Github.

I also provide an editor script on Github, but here is the basic function used.

public void Clean(Transform obj) {
  GameObjectUtility.RemoveMonoBehavioursWithMissingScript(obj.gameObject);
  for (int i = 0; i < obj.childCount; i++) {
    Clean(obj.GetChild(i));
  }

}