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.
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: