Unity 5.5.1 - Get the name of the last level played

Well, this should be much simpler than it is! Or I’m blinded by my own ignorance!

What I want is some means of getting the string name of the last playable level that was played.

To clarify:

  1. All playable levels are given sequential names - “Level_01” thru “Level_100”.
  2. All non-playable scenes are given names such as “Start Menu” and “LevelCompleted” these scenes contain buttons like “Main Menu” and “Replay Last Level”.
  3. I am NOT using the UI system and do not wish to do so at all. Rather, I have set up objects with colliders and am using my game mechanics to act as “buttons”. For example, when the player shoots the button called “Replay Last Level” the OnCollisionEnter2D should load the last level played.
  4. Whenever a level is completed or lost by the player they are taken to “LevelCompleted” and shown some game statistics (scores, times, etc.). They are given three choices: “Main Menu”, “Replay Last Level” and “Continue”.

What I need is simply a string variable that always contains the name of the previously loaded PLAYABLE level (i.e. those with names like “Level_01” and not like “Start Menu”. )

Then I will use that string to enter into my GameManager script in the appropriate place. The last level played will load when “Replay Last Level” is shot by the player. When the player shoots “Continue” I want to play the next playable level.

So pretty simple concept: I just lost “Level_02”. I am now in “LevelCompleted”. I shoot “Replay Last Level”. I enter “Level_02” again. If, instead, I shoot “Continue” it takes me to the next playable level, in this case, “Level_03” if that level has been unlocked. If it has not been unlocked then the “Continue” button will take me back to “level_02”.

BTW, I have a GameManager Script on an otherwise empty game object. It is a singleton that persists to every scene. Within this script are two classes - one is the main public class that is the singleton and does a whole bunch of stuff with data in larger arrays - the other is simply a serializable “public Class GameData”. It contains only variables that are written to a file and then reload as needed during the game.

Any help in this will be appreciated.

Assuming you use SceneManager, you can use it’s GetActiveScene function. Inside your playable scenes, just set some static variables on your GameManager. Or if the GameManager you are using loads the scenes, just save the name of the level to a static variable right before loading the level.