Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save isaveu/e2057f397f26df882b5b6e3d94556230 to your computer and use it in GitHub Desktop.
Save isaveu/e2057f397f26df882b5b6e3d94556230 to your computer and use it in GitHub Desktop.
Find all missing scripts in unity3d scene
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using UnityEngine.SceneManagement; //3
public class SelectGameObjectsWithMissingScripts : Editor
{
[MenuItem("Tools/WPAG Utilities/Select GameObjects With Missing Scripts")]
static void SelectGameObjects()
{
//Get the current scene and all top-level GameObjects in the scene hierarchy
Scene currentScene = SceneManager.GetActiveScene();
GameObject[] rootObjects = currentScene.GetRootGameObjects();
List<Object> objectsWithDeadLinks = new List<Object>();
foreach (GameObject g in rootObjects)
{
//Get all components on the GameObject, then loop through them
Component[] components = g.GetComponents<Component>();
for (int i = 0; i < components.Length; i++)
{
Component currentComponent = components[i];
//If the component is null, that means it's a missing script!
if (currentComponent == null)
{
//Add the sinner to our naughty-list
objectsWithDeadLinks.Add(g);
Selection.activeGameObject = g;
Debug.Log(g + " has a missing script!");
break;
}
}
}
if (objectsWithDeadLinks.Count > 0)
{
//Set the selection in the editor
Selection.objects = objectsWithDeadLinks.ToArray();
}
else
{
Debug.Log("No GameObjects in '" + currentScene.name + "' have missing scripts! Yay!");
}
}
}
@nopjia
Copy link

nopjia commented Jan 28, 2022

You should use GetComponentsInChildren to find all nested components under GameObject.

@RomainBitard
Copy link

You should use GetComponentsInChildren to find all nested components under GameObject.

Doing that works to find missing scripts but the selection breaks (it selects the parent (which can be very high in the hierarchy) and not the gameobject with missing script)

@RomainBitard
Copy link

@CatDarkGame
Copy link

Absolute garbage

@CatDarkGame
Copy link

#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using UnityEngine.SceneManagement; //3

public class SelectGameObjectsWithMissingScripts : Editor
{
[MenuItem("Tools/WPAG Utilities/Select GameObjects With Missing Scripts")]
static void SelectGameObjects()
{
List allObjects = new List();
foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(true))
{
allObjects.Add(obj);
}

    List<Object> objectsWithDeadLinks = new List<Object>();
    foreach (GameObject g in allObjects)
    {
        Component[] components = g.GetComponents<Component>();
        for (int i = 0; i < components.Length; i++)
        {
            Component currentComponent = components[i];
            if (currentComponent == null)
            {
                objectsWithDeadLinks.Add(g);
                Debug.Log(g + " - Find Fucking issue object", g);
            }
        }
    }
    if (objectsWithDeadLinks.Count > 0)
    {
    }
    else
    {
        Debug.Log("Not Found");
    }
}

}
#endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment