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.
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.
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.
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().