Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Remove all missing reference Behaviours

Discussion in 'Editor & General Support' started by thienhaflash, Dec 21, 2014.

  1. thienhaflash

    thienhaflash

    Joined:
    Jun 16, 2012
    Posts:
    512
    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.
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,102
  3. thienhaflash

    thienhaflash

    Joined:
    Jun 16, 2012
    Posts:
    512
    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
     
  4. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,102
    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)
     
  5. thienhaflash

    thienhaflash

    Joined:
    Jun 16, 2012
    Posts:
    512
    Tried that before, but those components are already null, you can not destroy them :(
     
    trappist-1 likes this.
  6. Danny_HoW

    Danny_HoW

    Joined:
    May 4, 2012
    Posts:
    47
  7. ferretnt

    ferretnt

    Joined:
    Apr 10, 2012
    Posts:
    331
    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.
     
  8. Brogan89

    Brogan89

    Joined:
    Jul 10, 2014
    Posts:
    165
    Capture.PNG

    Looks like I have to go through them all manually :(
    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 :)
     
    Last edited: Feb 10, 2018
  9. Danny_HoW

    Danny_HoW

    Joined:
    May 4, 2012
    Posts:
    47
    hehe no problem, glad it helped :)
     
  10. Saeleas

    Saeleas

    Joined:
    Sep 23, 2017
    Posts:
    11
    Did you find any way to fix that error? I'm having the same issue.
     
  11. ferretnt

    ferretnt

    Joined:
    Apr 10, 2012
    Posts:
    331
    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.
     
    chriszul and fffMalzbier like this.
  12. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    374
    Last edited: Dec 29, 2019
    Fenj and Brogan89 like this.
  13. Schmaggis

    Schmaggis

    Joined:
    Sep 2, 2017
    Posts:
    2
    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!
     
  14. chechoggomez

    chechoggomez

    Unity Technologies

    Joined:
    Feb 25, 2013
    Posts:
    89
    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:

     
  15. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,427
    Do you mind to share the script? this new Prefab workflow API confused me
     
  16. Brogan89

    Brogan89

    Joined:
    Jul 10, 2014
    Posts:
    165
    Code (CSharp):
    1.     public class RemoveMissingScripts : Editor
    2.     {
    3.         [MenuItem("GameObject/Remove Missing Scripts")]
    4.         public static void Remove()
    5.         {
    6.             var objs = Resources.FindObjectsOfTypeAll<GameObject>();
    7.             int count = objs.Sum(GameObjectUtility.RemoveMonoBehavioursWithMissingScript);
    8.             Debug.Log($"Removed {count} missing scripts");
    9.         }
    10.     }
     
    Ghosthowl likes this.
unityunity