Returns the name of the Scene that is currently active in the game or app.
The Scene.name returns a run-time, read-only, string value. The name limits to 244 characters. The Scene name defaults to scene
. The user changes the name during game creation.
The following script example changes the Scene depending on GUI.Button clicks and the name of the Scene. To make this example work:
Create a Project with two Scenes, scene1
and scene2
.
Attach the script below to a GameObject added to scene1
.
Attach the same script to a GameObject added to scene2
.
Click on the GameObject and go to the Inspector.
In the My First Scene
field and My Second Scene
fields, enter the names of the Scenes you would like to switch between, scene1
and scene2
.
Select scene1
by double-clicking it in the Project, and press Play
. The scene1
scene will appear.
Click the Load Next Scene
button and scene2
will be loaded.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class Example : MonoBehaviour { // These are the Scene names. Make sure to set them in the Inspector window. public string myFirstScene, mySecondScene;
private string nextButton = "Load Next Scene"; private string nextScene; private static bool created = false;
private Rect buttonRect; private int width, height;
void Awake() { Debug.Log("Awake:" + SceneManager.GetActiveScene().name);
// Ensure the script is not deleted while loading. if (!created) { DontDestroyOnLoad(this.gameObject); created = true; } else { Destroy(this.gameObject); }
// Specify the items for each scene. Camera.main.clearFlags = CameraClearFlags.SolidColor; width = Screen.width; height = Screen.height; buttonRect = new Rect(width / 8, height / 3, 3 * width / 4, height / 3); }
void OnGUI() { // Return the current Active Scene in order to get the current Scene name. Scene scene = SceneManager.GetActiveScene();
// Check if the name of the current Active Scene is your first Scene. if (scene.name == myFirstScene) { nextButton = "Load Next Scene"; nextScene = mySecondScene; } else { nextButton = "Load Previous Scene"; nextScene = myFirstScene; }
// Display the button used to swap scenes. GUIStyle buttonStyle = new GUIStyle(GUI.skin.GetStyle("button")); buttonStyle.alignment = TextAnchor.MiddleCenter; buttonStyle.fontSize = 12 * (width / 200);
if (GUI.Button(buttonRect, nextButton, buttonStyle)) { SceneManager.LoadScene(nextScene); } } }
The following example using two scenes, and one of them has a long Scene name with 244 digits. The other is called testScene
. To make this example work:
1. Create a new Project.
2. Change the name of the default scene to testScene
by selecting it and then use Assets->Rename.
3. Next, create a second scene and again select it and use Asset->Rename. Use the name as shown below. (This is the 244 character name "0123456789...0123").
4. Create a C# Script and call it Example.cs
.
5. Add the following script text to Example.cs
.
6. Next add an empty GameObject called GameObject
to each of the two scenes.
7. Finally copy Example.cs
to each of the two GameObjects.
Use the Game
button to launch the testScene
scene. A GUI Button is shown which allows the scenes to swap.
using UnityEngine; using UnityEngine.SceneManagement;
// SceneManagement.SceneManager-name example
public class Example : MonoBehaviour { private Scene scene;
void Start() { scene = SceneManager.GetActiveScene(); Debug.Log("Name: " + scene.name); }
void OnGUI() { if (GUI.Button(new Rect(10, 10, 150, 100), "Change Scene")) { if (scene.name == "testScene") { // The scene to load has a 244 characters name. SceneManager.LoadScene("0123456789012345678901234567890123456789" + "012345678901234567890123456789012345678901234567890123456789" + "012345678901234567890123456789012345678901234567890123456789" + "012345678901234567890123456789012345678901234567890123456789" + "012345678901234567890123"); } else { SceneManager.LoadScene("testScene"); } } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.