Skip to content

Instantly share code, notes, and snippets.

@EricHu33
Last active July 3, 2024 21:34
Show Gist options
  • Save EricHu33/1020f1b36c0fcb646fb5d8931c31dd08 to your computer and use it in GitHub Desktop.
Save EricHu33/1020f1b36c0fcb646fb5d8931c31dd08 to your computer and use it in GitHub Desktop.
Clean all missing scripts in Unity Gameobject, attached this script into the root of the gameobject. right click and call "RemoveMissingScript"
[ExecuteInEditMode]
public class YourComponent : MonoBehaviour
{
[ContextMenu("remove missing script")]
public void RemoveMissingScript()
{
var transforms = GetComponentsInChildren<Transform>(true);
foreach (var childTransform in transforms)
{
var components = childTransform.GetComponents<Component>();
var hasMissingScript = false;
for (int i = 0; i < components.Length; i++)
{
if (components[i] == null)
{
hasMissingScript = true;
break;
}
}
if (hasMissingScript)
{
GameObjectUtility.RemoveMonoBehavioursWithMissingScript(childTransform.gameObject);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment