Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ardaerbaharli/e02c9a867593dfe86027b727fa4703f7 to your computer and use it in GitHub Desktop.

Select an option

Save ardaerbaharli/e02c9a867593dfe86027b727fa4703f7 to your computer and use it in GitHub Desktop.
Select GameObjects With Missing Scripts
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using UnityEngine.SceneManagement; //3
public class SelectGameObjectsWithMissingScripts : Editor
{
[MenuItem("Tools/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();
//Create a list to store the GameObjects with missing scripts
List<GameObject> gameObjectsWithMissingScripts = new List<GameObject>();
// Iterate through all level GameObjects in the scene hierarchy with a recursive function
foreach (GameObject rootObject in rootObjects)
{
IterateThroughChildren(rootObject, gameObjectsWithMissingScripts);
}
//Select all GameObjects with missing scripts
Selection.objects = gameObjectsWithMissingScripts.ToArray();
}
private static void IterateThroughChildren(GameObject rootObject, List<GameObject> gameObjectsWithMissingScripts)
{
// Iterate through all level GameObjects in the scene hierarchy with a recursive function to find GameObjects with missing scripts
foreach (Transform child in rootObject.transform)
{
var components = child.gameObject.GetComponents<Component>();
//If the GameObject has a missing script, add it to the list
foreach (var c in components)
{
if (c == null)
{
Debug.Log("Found GameObject with missing script: " + child.gameObject.name);
gameObjectsWithMissingScripts.Add(child.gameObject);
}
}
//If the GameObject has children, recursively iterate through them
if (child.childCount > 0)
{
// Debug.Log("Iterating through children of: " + child.gameObject.name);
IterateThroughChildren(child.gameObject, gameObjectsWithMissingScripts);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment