- Home /
how to Undo DontDestroyOnLoad
Very obvious case:
Game starts,
Start screen Level is loaded and makes a GameObject undestroyable on Load Level,
another level is loaded and stores data in the GameObject, and another one and another one...
at some point the player clicks on the "reset" button (availabe in each level) to bring him back to the Start screen Level, where everything should be as it was right after the Game started.
Now, the logical thing would be to somehow undo the DontDestroyOnLoad state of that GameObject and then call Application.LoadLevel("Start_screen_level"); .
But I just can´t find a way to undo DontDestroyOnLoad. Is there really no way to do this? (It seems to be such a basic and logical thing to have.)
Can't you just manually call `Destroy` on the object in question?
Damn I knew this would come... I think this is a dirty solution. And yes I tried this but it did not work. Directly after the destroy() I could still read my data from the Object.
Answer by shaderop · Sep 15, 2012 at 12:02 PM
As I understand it, `Destory` makes the object for deletion at the end of the current frame. But you can use `DestroyImmediate` if that is not acceptable.
Also, did you consider manually resetting the object state instead of destroying it and then recreating it on restart? As in set all its fields to defaults or null, zero any collections it has, etc? Could net you a slight performance boost this way.
Your answer
Follow this Question
Related Questions
Undo DontDestroyOnLoad? 2 Answers
Preventing variable from resetting when level loads 1 Answer
reload level whilst saving certain objects, without dontdestroy() 2 Answers
Is OnLevelWasLoaded supposed to be called twice when using DontDestroyOnLoad? 3 Answers
DontDestroyOnLoad does not work after reload the scene? 1 Answer