How do I programmatically find name of missing script?

When I insantiate an object which has a missing component script, it is clear that Unity knows the name of the missing script as it shows it in the Inspector:


However, when I get all the components with GetComponents() the two missing scripts are returned as null values.

How do I get the Component information on these missing components? Also how can I remove them programmatically from the GameObject?

The Inspector does all of this so there must be a way.

@JeffKesselman How do I Remove null components ( i.e. "Missing(Mono Script)" ) via editor script? - Unity Answers

This is a little old but I would think it still stands.


On second thought, there could be a solution. Scenes are kept in YAML format which is just a text file.

Here is what a Script looks like:

  m_ObjectHideFlags: 0
  m_PrefabParentObject: {fileID: 0}
  m_PrefabInternal: {fileID: 0}
  m_GameObject: {fileID: 1752247963}
  m_Enabled: 1
  m_EditorHideFlags: 0
  m_Script: {fileID: 11500000, guid: 2b6c2930ee1cdeb42a78bab9b6e97944, type: 3}
  some: 10
  other: 20

It contains some info and the public members. Scripts within the object qre separated by

--- !u!012 &0123456

with the first value for the object and the second increases from component to component attached to the object.

Now the problem is that nothing shows on the YAML that the script is missing, it does not even indicate the name.

Now the long way would be using reflection and comparing public variables from component on the object and variable name on the YAML. If no match, remove the component from YAML which will automatically remove the missing component from Unity.

You could get all component types on object:

Type[] types = gameObject.GetAllTypeOnObject();

Those would be extension methods:

public static class Utility{
    public static Type[] GetAllTypeOnObject(this GameObject go){
         List<Type> typeList = new List<Type> ();
         Component[] comps = go.GetComponents <Component>();
         foreach (Component c in comps) 
              typeList.Add (c.GetType());
         return typeList.ToArray ();

Now you get all of the Components attached to the GO.

From there you can get all public fields with

foreach(Type t in types){
     FieldInfo [] myFieldInfo = t.GetFields(BindingFlags.Instance
        | BindingFlags.Public);

     // Access the YAML file with basic StreamReader File IO
     // This requires to know where the Scene file are stored in Project 
     // Create a parser that retrieves all field name.
     // They seem to always be stored after m_EditorClassIdentifier: 
     // and before --- !u! or end of file
     // Compare with Name contained in FieldInfo. 
     // Delete the component in the file, rewrite the file with StreamWriter and close

     // If Editor script

Assets that can be used to find references and missing have been released.

You can find and fix missing components or references.

alt text

It’s FREE!
Please manage your assets conveniently and solve the problem of your project.

Detailed explanation is in the link below.