Unity3D How To Get Current Scene Name

Unity3D How To Get Current Scene Name

I created a simple countdown timer that jumped to the next scene. I wanted to reuse the same script for this effect, two times. Instead of rewriting the same script, I needed to detect the current scene name so the script knows where to jump next. Here is how:

Include The Namespace

Include the SceneManagement namespace in your project:

using UnityEngine.SceneManagement;

Get The Current Scene Name

Using the Start function you can grab the current scene name when the scene starts:

string currentScene;
void Start() {
     currentScene = SceneManager.GetActiveScene().name;
}

Load The Target Scene

Now that you have the current scene name, you can figure out what to do in your loop:

if( currentScene == "Intro" ) {
     SceneManager.LoadScene("MainMenu");
} else {
     SceneManager.LoadScene("Intro");
}

I was confused for a sec that the .name was a member of GetActiveScene() but it was cleared up when I found an example on the Unity3D forum.