- Home /
Pause level, push on stack, load another level, pop???
Anybody know if I can do this?
So to explain in more detail:
I want to allow the user to bring up a pause menu, which is just an overlay. Then, if they press something such as an options button, I load the options level but I don't destroy the current level, push the new options level onto "the stack", they do their thing, once they press "back" it will "pop" the options level off the stack and resume the level where it was paused at (the pause menu).
I like unity...but I'm a c++ programmer used to customizing what I need, and so far I don't see that it has this capability. Can anyone show me otherwise?
Thank you.
Answer by japanitrat · Sep 15, 2011 at 09:29 AM
The problem with loading new levels is that the contents will be mixed with what you already have in the scene. You could implement your own stack by tracking everything that is new in the scene after you loaded a new level. So if you do a pop, you just remove those items again. I do not recommend it though, since your original level might spawn new objects in between, and those would be lost after a pop, too.
I suggest you put your options stuff into a prefab that you can add and remove easily from the current scene (or just show and hide). Prefabs are designed to provide a predefined collection of objects and behaviours in a single object which you can instantiate as you like at runtime.
Yeah, I think the prefab is the way to go. Straightforward and clean. I was contemplating that, but it will mess with my current setup a bit, but in the end I think I'll be glad I did it. Thanks!
Answer by mohanrao164 · Sep 15, 2011 at 09:22 AM
We can do this by using delegats method.
try this link it will help u:
I suspect this has nothing to do with the main subject (change to a new scene and return to the previous scene), but for sure is a very valuable hint - upvoted because of this hint!
This is good, delegates are nice, but it doesn't solve all the issues with switching actual levels. I'll definitely keep this in $$anonymous$$d for the future though, thanks.
Answer by aldonaletto · Sep 15, 2011 at 12:18 PM
I would create a "room" far away from the main scene (say, x = 10000) and place all items of this pause scene there. When entering the pause scene, I would set a pause flag (to stop some critical jobs, like time counting) and teleport the player to this "room"; when exiting the pause scene, teleport the player back to its original position and clear the pause flag.
This pause scene should be created in the very first level (centered far away from 0,0,0). I would child all its objects to an empty object and call DontDestroyOnLoad for it (all children are affected), so when loading a new level this "pause room" would be kept alive. Some common items, like the RenderSettings, probably would have to be saved, switched to the pause room settings, and restored on return (specially the skybox, fog and ambient light settings).
This isn't a bad idea, but I think it would get messy. I think I'll shoot for the prefab like japanitrat suggested. That sounds nice and clean. Thanks!