Search Unity

Question Removing components from Editor Script and using Undo.RegisterUndo causes Console Errors, But Why?

Discussion in 'Scripting' started by ClearRoseOfWar, Dec 7, 2022.

  1. ClearRoseOfWar

    ClearRoseOfWar

    Joined:
    Sep 6, 2015
    Posts:
    89
    Hello fellow devs,

    I am creating an editor script that allows me to quickly remove components (which works fine for the most part honestly), but I'm failing to understand one thing about it (despite my hours of research and tinkering):

    Why is it when I Undo after using to remove components from the default MainCamera GameObject, I get the error:

    GameObject 'Main Camera' does not reference component AudioListener. Fixing.
    UnityEditor.EditorApplication:Internal_CallGlobalEventHandler ()


    Keep in mind, the components are restored! Perhaps its a frame issue?


    Code (CSharp):
    1.  
    2. for (int i = 0; i < Selection.gameObjects.Length; i++) {
    3.     Component[] listofComps = Selection.gameObjects[i].GetComponents<Component>();
    4.     Undo.RegisterCompleteObjectUndo(listofComps, "Remove Componenets");
    5.     foreach (var comp in listofComps)
    6.         if (!(comp is Transform))
    7.             DestroyImmediate(comp);
    8. }
    9.  
     
  2. ClearRoseOfWar

    ClearRoseOfWar

    Joined:
    Sep 6, 2015
    Posts:
    89
    Perhaps I could just suppress Error messages in that frame? But I'm still at a loss since it doesn't provide an error code, but rather, its just a Debug.LogError, and I can't open it to view the code that creates the error..

    How can I suppress Error messages?
    OR how can view the code its generated from?