Is there any better way to "reload the current level"?

I’m using a single Player object and a single Camera object for multiple levels of a game to be able to change seamlessly from one to another maintaining the settings and some variables (a countdown time is the most relevant).

To accomplish this I used the:

		DontDestroyOnLoad(transform.gameObject);

…on both the playerController and cameraController, so when the player dies and I reload the level I also need to manually destroy both instances so that they don’t duplicate.

The problem comes when I die outside of the initial Level, because the objects get destroyed and don’t spawn at the start of that level.

I’m thinking about doing a more elaborated restartGame function, so that I store the starting coordinates of all levels… but I’m also wondering if there is a better way to do all of this, and since I don’t want to mess my code up too much I would like to know if any of you knows about a better way to do all this.

Greetings.

Instead of loading the player and camera in the first actual level you can use another Startup level which only has your player and camera and a script to load the first level. This way when reloading level one or any other level, you are not loading the Startup level and your player and camera will not be duplicated.

I would suggest you to establish a session (custom script using Singleton concept) and save your settings in this object. Don’t use DontDestroyOnLoad since every scene load will produce at least a Camera., so you must have to destroy it if you want to keep the first camera only.

Lets first have a look at session object say GameSession:

using System;

public sealed class GameSession
{
   private static volatile GameSession instance;
   private static object syncRoot = new Object();

   public int currentLevel;
   // declare you remaining variables here to maintain game settings.
   // possibly storing coordinates

   private GameSession() {}

   public static GameSession Instance
   {
      get 
      {
         if (instance == null) 
         {
            lock (syncRoot) 
            {
               if (instance == null) 
                  instance = new GameSession();
            }
         }

         return instance;
      }
   }
}

you can use it like:

GameSession.Instance.currentLevel = 1;

If you want to maintain this session in upcoming session, you mark this class serializable and store it in a file, and load file in the next session, which is a different story.

Now at this point you should write script in a way that you make just one Scene say GameScene and there setting will be retrieved from this session object described above. e.g.,

Script on camera:

int currentLevel;
GameObject enemyPrefab;
public GameObject[] prefabCollection; // collection of enemy prefab to be instantiated on each level.

void Start()
{
       currentLevel  = GameSession.Instance.currentLevel;
      enemyPrefab =  Instantiate(prefabCollection[currentLevel]) as GameObject;     
}

This way, on completion of first level you have to load you new level like this:

void CustomGameCompletedEvent()
{
     // Setup settings for new level
     GameSession.Instance.currentLevel = GameSession.Instance.currentLevel + 1;

     // Reload the same scene with different settings
     Application.LoadLevel("GameScene");
}