Skip to content

Instantly share code, notes, and snippets.

@mandarinx
Created December 11, 2014 08:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandarinx/4c08c8c6f5b1fca4cd65 to your computer and use it in GitHub Desktop.
Save mandarinx/4c08c8c6f5b1fca4cd65 to your computer and use it in GitHub Desktop.
Find Missing Component References
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class FindMissingComponentRefs : System.Object {
[MenuItem("Tools/Find Missing Component References")]
public static void FindMissingRefs() {
bool found = false;
List<Component> components = GetComps<Component>();
foreach (Component c in components) {
SerializedObject so = new SerializedObject(c);
SerializedProperty sp = so.GetIterator();
while (sp.NextVisible(true)) {
if (sp.propertyType == SerializedPropertyType.ObjectReference) {
if (sp.objectReferenceValue == null &&
sp.objectReferenceInstanceIDValue != 0) {
ShowError(FullObjectPath(c.gameObject), sp.name);
found = true;
}
}
}
}
if (!found) {
Debug.Log("All component references are OK");
}
}
private static void ShowError(string objectName, string propertyName) {
Debug.LogError("Missing reference found in: " + objectName +
", Property : " + propertyName);
}
private static string FullObjectPath(GameObject go) {
return go.transform.parent == null ?
go.name :
FullObjectPath(go.transform.parent.gameObject) + "/" + go.name;
}
public static List<T> GetComps<T> () where T : Component {
List<T> list = new List<T>();
UnityEngine.Object[] gos = GOs;
foreach (GameObject go in gos) {
T comp = go.GetComponent<T>();
if (comp != null) {
list.Add(comp);
}
}
return list;
}
public static GameObject[] GOs {
get {
UnityEngine.Object[] objs = UnityEngine.Object.
FindObjectsOfType<GameObject>();
return objs as GameObject[];
}
}
}
@mandarinx
Copy link
Author

This is a slightly modified version of http://www.tallior.com/fixing-missing-references/.

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