I made a simple menu system, when clicked on a button to open another screen, I first try to clear current UI.Root and then open a new screen. However, sometimes it crashes throwing the "Collection was modified; enumeration operation may not execute." error and sometimes it works but then some UIElements lose their events, don't register clicks anymore.
So I guess I shouldn't be clearing UI.Root in a button click event, should delay it one frame maybe?
Right now on button Released event I'm calling:
(window is a child of UI.Root)
then in another class, adding new screen
window = new Window();
Also something wrong happens with Scene when trying to restart a game, same way, on a button click. It loads fine, but after couple times restarting, scene stops calling some events randomly. For example these:
Scene.GetComponent().PhysicsPreStep += FixedUpdate;
Scene.ScenePostUpdate += PostUpdate;
I'm removing scene like this:
Then restarting by
Scene = new Scene();
in a completely new class instance.
So what would be the correct way to do things?