Undo.PerformUndo from onGUI

Has anyone gotten Undo.PerformUndo to not throw an error? My script works perfectly yet still throws this gem:

An example of something that will error for me when I hit the button:

[CustomEditor( typeof(blah) )]
public class blahEditor : Editor {
	
	public override void OnInspectorGUI(){

		if( GUILayout.Button(opicon, buttonStyle1) ){
			Undo.SetTransformParent(sometransform, mommytransform, "blah blah");
			Undo.PerformUndo();
		}
	}
}

Can you please wrap your GUI button code into a try catch block , and Debug the InvalidOperationException and then post it here. I never used Undo class nor i know how it exactly works , but maybe from more information i could tell you something.

While catching do something like this

  if( GUILayout.Button(opicon, buttonStyle1) )
{
           try
{
            Undo.SetTransformParent(sometransform, mommytransform, "blah blah");

            Undo.PerformUndo();
}
catch(Exception ex)
{
Debug.Log(ex.Message);
}

        }

Very interesting. Its passing right through try and not throwing the catch. The errors remain the same and the script carries out as usual. If I take the Undo.PerformUndo() out but still record the Undo actions, manually pressing undo immediately after the script is finished doesn’t create errors. It just undoes my script’s doings.

Also the error only happens the first time. I can run the script back to back and won’t get the error the second time. It only happens the first time running since clearing the console.

Probably because there’s nothing to Undo on the first start . You can see that the error appears at the System.Collections.Stack.Peek. I’d say just ignore the error and carry on.

I don’t really know how Undo class really works , but i can guess that it’s storing actions in LIFO stack and on the function call it just undoes the last action (the last action added to the stack).

In case of having no actions done , it’s trying to peek at the stack but getting no results and throws that weird error.

hmm, its definitely undoing stuff the first time around, else my script wouldn’t function right. I’d be okay with not using undo at all if I could just figure out how to destroy an object then bring it back. The function of my script is to save a selection of objects as a new scene, leaving the current scene unaltered. So I’m destroying everything that’s not in the selection, saving the scene, then undoing the destroy.

You can achieve that by creating a temporary object , and store your current object into that , destroy the object and then instantiate the temporary object again.

private GameObject _tempGO;

void YourLogic()
{
   //your logic
   //right before the destroy store the gameobject into _tempGO
    _tempGO = gameObjectToDestroy;
    Destroy(gameObjectToDestroy);
}

void BringBackToLife()
{
      GameObject go;
     go = (GameObject)Instantiate(_tempGO,_tempGO.transform.position,_tempGO.transform.rotation);
}

edit : You can also keep a track of all objects you want to “ressurect” in a generic list or an array. You can also make your logic on how to serialize those and even after the exit/quit you can still "ressurect them.

I’ve tried that before and I just tried that again. I think perhaps that doesn’t work for me because I’m using DestroyImmediate() as this is an editor extension. I think maybe DestroyImmediate() destroys all references too. I get:

Try storing that object in another class. Make a static variable GameObject in any other class and store it there.

No luck. I’m in quite the pickle.

I have the same issue, Unity is throwing me that error with Undo.PerformUndo(); and I dont know why

Hello, I know this is an old post, but did any of you come with a satisfactory answer?

We are now trying with EditorApplication.DelayedCall. Seems promising, but there may be better options. Any thoughts?