Remove all missing reference Behaviours

Hello everyone,

Does anyone know how to remove all missing monobehaviours in the scene and all prefabs by editor script ? I did do some search but still not find a way to do. Is it doable ? Can anyone point me to the right direction ?

Thanks a lot.

You can use the FindMissingScriptsRecursively.cs to find all missing Behaviours in the scene.
http://wiki.unity3d.com/index.php?title=FindMissingScripts (wiki.unity3d.com is offline now)
Just select all objects / or the object root and open Window/FindMissingScriptsRecursively

I can find it, actually. Just don’t know a way to remove those missing behaviours but manually do it one by one.

Thanks for your help anyway

You could add somting like DestroyImmediate(components*); to line 51 of the script , that should delete the missing component entry (NOT TESTED USE WITH CAUTION)*

Tried that before, but those components are already null, you can not destroy them :frowning:

1 Like
3 Likes

Even after removing the interpolated strings so I can run it on the version of Mono we’re using, that didn’t work for me in 2017.3. It does clearly remove many behaviours (reading the diff of the scene), but quite a few then get added back when you hit play with the message:

CheckConsistency: GameObject does not reference component MonoBehaviour. Fixing.

3387350--266194--Capture.PNG

Looks like I have to go through them all manually :frowning:
Don’t change Nested Prefab library mid project people. It makes for a long day.

Edit:
It actually did delete them, it just didn’t print properly. Thanks for the code mate :slight_smile:

1 Like

hehe no problem, glad it helped :slight_smile:

1 Like

Did you find any way to fix that error? I’m having the same issue.

No, it’s on the very long list of things we’ve just learned not to care too much about. There are many such things in the life of an app developer. Our iTunes Connect console is filled with dead apps that we have no way to remove. Our older codebases are filled with zombie monobehaviours. Xcode likes to make a new provisioning profile every time you breathe. The only way forward is just to not care and keep on shipping stuff.

2 Likes

This answer: http://answers.unity.com/answers/1614734/view.html
Which relies on GameObjectUtility.RemoveMonoBehavioursWithMissingScript and PrefabUtility.GetCorrespondingObjectFromSource (available in 2019.1)

6 Likes

Hello!
Sadly the Code does not find any Missing Scripts :-/

int count = GameObjectUtility.GetMonoBehavioursWithMissingScriptCount(gameObject);
Debug.Log("Found {count} missing scripts"); count = GameObjectUtility.RemoveMonoBehavioursWithMissingScript(gameObject); Debug.Log(“Removed {count} missing scripts”);

Count is always 0 even if gameObject does have “null-scripts”.

With the old method it still finds the missing scripts but the “prop.DeleteArrayElementAtIndex” ist nit supported any more in Unity 2019… Is there another way to delete missing scripts from the serializedObject-Property “m_Component”?
Thanks!

Hi @Schmaggis , which version of Unity are you using?
I just tried the code you are using and this is the result I am getting with 2019.2.15f1:

Do you mind to share the script? this new Prefab workflow API confused me

    public class RemoveMissingScripts : Editor
    {
        [MenuItem("GameObject/Remove Missing Scripts")]
        public static void Remove()
        {
            var objs = Resources.FindObjectsOfTypeAll<GameObject>();
            int count = objs.Sum(GameObjectUtility.RemoveMonoBehavioursWithMissingScript);
            Debug.Log($"Removed {count} missing scripts");
        }
    }
19 Likes

Thank you so much Brogan89, you are a lifesaver, I’ve seen so many complicated methods that just couldn’t get the job done and you have found the simplest solution. Thanks again and God bless!

1 Like

FYI, non of those presented options here works in some edge cases.

This is the result of Brogan89 script, nothing helps in my case :frowning:
I think I need to go through 600 missing references manually :confused:
6897806--807161--yyy.gif

Yeah I just had this happen as well. It removed the scripts from the prefabs but once I saved it reverted all the missing scripts back :eyes:

However, I have another window I made which may be of worth to you. It just helps find the prefabs or whatever with missing scripts.

using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class FindMissingScriptsWindow : EditorWindow
{
   [MenuItem("Window/Missing Script Window")]
   private static void Init()
   {
      GetWindow<FindMissingScriptsWindow>("Missing Script Finder").Show();
   }

   public List<GameObject> results = new List<GameObject>();

   private void OnGUI()
   {
      if (GUILayout.Button("Search Project"))
         SearchProject();
      if (GUILayout.Button("Search scene"))
         SearchScene();
      if (GUILayout.Button("Search Selected Objects"))
         SearchSelected();
    
      // src: https://answers.unity.com/questions/859554/editorwindow-display-array-dropdown.html
      var so = new SerializedObject(this);
      var resultsProperty = so.FindProperty(nameof(results));
      EditorGUILayout.PropertyField(resultsProperty, true);
      so.ApplyModifiedProperties();
   }
 
   private void SearchProject()
   {
      results = AssetDatabase.FindAssets("t:Prefab")
         .Select(AssetDatabase.GUIDToAssetPath)
         .Select(AssetDatabase.LoadAssetAtPath<GameObject>)
         .Where(x => IsMissing(x, true))
         .Distinct()
         .ToList();
   }

   private void SearchScene()
   {
      results = FindObjectsOfType<GameObject>()
         .Where(x => IsMissing(x, false))
         .Distinct()
         .ToList();
   }

   private void SearchSelected()
   {
      results = Selection.gameObjects
         .Where(x => IsMissing(x, false))
         .Distinct()
         .ToList();
   }

   private static bool IsMissing(GameObject go, bool includeChildren)
   {
      var components = includeChildren
         ? go.GetComponentsInChildren<Component>()
         : go.GetComponents<Component>();
    
      return components.Any(x => x == null);
   }
}
1 Like

I have an error message that just lists the missing script, “Error while saving prefab: ‘Assets/XXXX/XXXXX/etc’” and says “You are trying to save a Prefab with a missing script. This is not allowed.” Well, I found its folder. Couldn’t I just go into the asset folder and delete its file? (Edit: All I’ve done so far is try importing it, beyond that I haven’t done anything with the asset except download it from the assetstore)