- Home /
How clean the level before loading another one?
Hi, I have a problem, I'm making a game where the level is generated randomly whenever the player plays a new game (like Diablo II), when the player dies the application loads another level in order to the user to pick a new character then bring the player to accion again loading the previous level again, now, I want to do this... I do managed the problem of the variables and things, however. The "scenery" is not only generated randomly it will also appears and disappears when the player gets closer or away, just like the Render distance in most games, this is for optimizing Mobile devices. The problem is. the level is not cleaned when it's loaded for second time, so is not even generated again, but also the loaded prefabs(entities) remain. I can delete the "map" with just fill a GameObject array with FindGameObjectsWithTag("MapBlock") for example and do Destroy() into a For loop but how can I properly delete also the small entities like Enemies, powerups, traps, etc. There are a lot of them, it could be so much work for mobile devices to do a massive Destroy() loop. I want to clean the whole scene when its Reloaded, or clean it before load another level, but leaving maybe one object in the scene in order to execute the scrip to re-generate the map and respawn entities.
Answer by TonyLi · Jun 07, 2013 at 01:39 AM
Make all of those objects children of a single root object. Let's say you call the object "Level". To clear the scene, just destroy Level. To generate a new scene, add the generated objects as children of Level. Other objects (say one is called NeverUnloadMe) won't be affected.
For example, your scene hierarchy could look like this:
NeverUnloadMe
Level
Scenery
map block 1
map block 2, ...
Dynamic
enemy 1
enemy 2, ...
projectile 1, ...
powerup 1, ...
This way you don't have to deal with LoadLevel().
Thanks I was thinking on something like that, maybe not make them all childrens of one big daddy but make every entity son of his "Scenery block" father, then delete only the map blocks at the end. and that should delete entities too.
And unfortunately I always need to deal with LoadLevel thing because of the variables and stuff that for some reason don't get executed on the Start function of the script the second time I ran the level
Answer by Dave-Carlile · Jun 06, 2013 at 08:56 PM
Create an empty level, and do a LoadLevel
on that before generating your scene. So your menu is one scene, and the game is the "empty" scene.
I don't understand, I have two Scenes one with the menu and one with the "game" where I'm generating the scenery and everything, I have one Script that generates the scenery with random "scenery blocks" and every "scenery block" have their own entity spawner, also random spawned (I mean some of them spawn enemies, some traps, some powerups and things like that). I can succesfully restart the Level from the level itself, and everything works like charm, but when I try to load (re-load actually) the level from the menu, everything mess up!. How I will manage to get an Empty scene everytime i want to recreate the scenery and the entities, isnt that what I just recently asked?
The empty scene is part of your project. Each time you do a LoadLevel
for that scene it will load the empty scene. It will start of empty, and you can then generate the game objects.
Answer by Noztradamuz · Jun 06, 2013 at 10:46 PM
Ok I managed to do this by doing this:
I moved every code line i was executing on the Start funcion on almost every script related to the Level, into a Function for example: Initialize() and i call that function into the Start() and OnLevelWasLoaded() functions, also i'm destroying the "scenery blocks" if there are any in the OnLevelWasLoaded() and Start() functs. then respawn them all again, I hope there's no junk left on the scene like missed entities or something that can make the game slower and slower the more they play.
Your answer
Follow this Question
Related Questions
Click handling at world scene level 1 Answer
How to save a Scene during a game? 1 Answer
Multiple Cars not working 1 Answer
Can't get simple script to work? 4 Answers
How Do You Have Multiple High Scores For 1 GameOverScene? 1 Answer