Hi everyone,
I am new to unity, I am able to load levels by some action in one level or completion of one level. My problem is i want to unload the scene and then want to load the other scene. I don’t know exactly whether the objects of current scene will be destroyed or not.( i see there is a method Destroy to destroy objects but i didn’t find any class to refer to a scene or level).
Actually i am loading one scene after an action is done in previous scene but by performing some actions on this scene i want to reload the previous scene at the state at which i left, for this i am using coroutine in that i am using time.timescale = 0; and i want to load the previous scene by destroying current scene.
My requirement is something like pause and resume of a game
Thanks for reply, May be i haven’t explained clearly because i am not good at English(presentation), I want to unload the scene that was presently above my required scene. i.e i want to destroy the current scene and resume previous scene.
It may be just me that oversimplyfies things, but could you not just load the first level again, with DontDestroyonLoad set on all objects that have been altered when you left the initial scene the first time ?
So you have level 1, and when that completes then you load level 2. Then go out of level 2 and back to level 1? This can be done by loading the levels each time (witch would “unload” the just left level) and if you want the chages made on the levels to be persistent you use DontDestroyOnLoad for those objects.
Alternatively you can just make two scenes in one scene, and switch camera (or camera positin) to switch between levels. There are many options.
If you look at the documentation Bulgroz linked for you, it states that you can load a scene by name or by index.
Loading that scene destroys the current scene. As you created the scenes, you know their names and the order in which they were assigned in the build process. So it’s a very simple matter of just loading your required scene by either method.
If you are using LoadLevelAdditive, the sop is to assign all the objects for that scene to a dummy GameObject hierarchy. Then one call to destroy unloads that scene.
If you can tell us what you’ve tried and why LoadLevel isn’t sufficient, we could give you some further pointers.
I am designing a game that is having levels, In a level if some action is performed a bonus level will appear, (This scene(bonus level) is common for some levels(say 3 levels). So i made it a separate scene) after completion of this level i want to resume previous level.(previous level is not completed before going to bonus level its just extra scene) When i am loading the past level using loadlevel, it is starting from the beginning, not resuming. I am using LoadlevelAsync for loading bonus level.
you will have to store the state or actually have the bonus level present in the scene but some other place, freeze the gameplay in the “previous and later area” while you are in the bonus area of the scene
What he is saying is that when you use LoadLevel (or LoadLevelAsync) instead of LoadLevelAdditive, it will unload the previous one. To keep the state or current conditions of your Level 1, but add the Bonus Level, you would need to load the Bonus Level with LoadLevelAdditive (does not unload previous level) or leave it how it is now with loading or unloading, but have a script that holds the data that you would need when going back to Level 1 from the Bonus Level. Data such as player position or life or enemies defeated or whatever things that you would need to re-apply to Level 1 to make it seem like you are returning to the level you just left.
Does that help? I know this is a bit late, but I was looking for some more in depth scene loading info and saw you were still hanging here.
The problem, if I understand it, is in relation to an “all or nothing” limitation in Unity’s level loading/unloading system. There appears to be no way of centralising management of level loading? Ideally one would like to have one scene that hangs around for the entire duration of the game, acting as a manager for all the other scenes, loading and unloading arbitrary scenes. But the way the system seems to works is that you can only load a scene (a level) it it either unloads all previously loaded scenes/levels (LoadLevel), or the new scene is added to all previously loaded scenes/levels (LoadLevelAdditive), neither of which appears to provide a way of any one scene to act as a manager for all others. One is forced (it would seem) to distribute level loading logic across all scenes involved in a game.
However the solution (as others have indicated) is to first load a scene in which a manager object is marked as ‘Don’t DestroyOnLoad’ and this would then persist no matter what new scenes are loaded (additively or otherwise). It can then safely act as a manager for loading all other scenes. If all subsequent scenes are loaded using LoadLevel (or LoadLevelAsync) they will conveniently destroy any previously loaded scenes, but not the manager object.
What kind of game / how big of game are we talking? I’ve had reasonable success in making games in a single scene using managers, pools and game objects as my scenes. Can easily disable one parent game object with it’s (level) children and enable another and whatever. But if your game is too large it isn’t really feasible since most everything is loaded in the beginning of the game(can always spawn level objects when it’s level game object is enabled… but not always the best.
Feel free to skim through the Scene Loader source code or watch the video tutorial that explains how it works, which is similar to what the folks above are talking about. It’s a manager that uses LoadLevelAdditive (or LoadLevelAdditiveAsync for Pro) to load and unload scenes.
For a more full-featured product, you could buy the excellent SECTR STREAM.