- Home /
 
10 levels in a 1 scene ?
Hi I m making a game its a space shooter game I want to 10 levels first but after I want to do it 100 levels. And 1 level per scene is very hard to save then I need to solve it. How can I make this system can you help me ?
Answer by pauldarius98 · Mar 03, 2021 at 11:40 AM
You could do a prefab for each level and using this way you need only 1 scene. Save your levels in something like Resources/Levels and give them a name convention like Level1, Level2, etc and then you can load any level at runtime like this:
 public class LevelsManager: MonoBehaviour
 {
     private GameObject currentLevel;
     
     public void LoadLevel(int levelNumber)
     {
         //remove the old levelNumber
         UnloadCurrentLevel();
         
         currentLevel = Instantiate(Resources.Load<GameObject>($"Levels/Level{levelNumber}"));
     }
     
     public void UnloadCurrentLevel()
     {
         if (currentLevel != null)
         {
             Destroy(currentLevel);
         }
     }
 }
 
              But do I have to assign this code to an object or something?
Answer by rh_galaxy · Mar 03, 2021 at 12:32 PM
The answer from pauldarius98 is excellent, but if you are doing a 100 levels it may be good to also think about if you can generate the levels from a (text) file instead of having the whole level to be in a prefab. You can describe the objects with position and other attributes and attributes of the level. If this is better or not, it depends of your project. This is for example better if you will want to add levels later without recompiling the game.
This is only suitable if you don't create the levels in the Unity editor, but generate the level from this text.
In my space-shooter game Galaxy Forces VR (open source) I have two scenes, Menu and Game, where in the Game-scene I generate the level my self in code by Instantiating objects (prefabs) in the scene on load.
This is an example of one of my levels to give you an idea. I then parse it on load to build the level.
 *MAPTYPE MISSION
 *MAPSIZE  32 32
 *MAPFILE  mission01.txt
 *TILESET  ts_evil.tga
 *PLAYERSTARTPOS   421   290
 *LANDINGZONE    96   220  3 CARGOBASE NOEXTRALIFE NOANTENNA 2 10 10
 *LANDINGZONE   832   220  3 CARGOBASE NOEXTRALIFE NOANTENNA 1 15
 *ENEMY 0
   *ANGLE         0
   *FIREINTERVAL  2000
   *SPEED         0
   *NUMWAYPOINTS  1
   *WAYPOINTSX      94
   *WAYPOINTSY     381
 *ZOBJECT 0   497   863 90
 *ZOBJECT 1   579   657 90
 
                 Your answer