name
Returns the name of the Scene that is currently active in the game or app.
Read time 3 minutesLast updated 10 days ago
Definition
- Type: Property
- Namespace: UnityEngine.SceneManagement
- Assembly: UnityEngine.CoreModule
public string name { get; set; }
Remarks
The name returns a run-time, read-only, string value. The Scene.name limits to 244 characters. The Scene name defaults to . The user changes the Scene.name during game creation.
sceneThe 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,and
scene1.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 thefield and
My First Scenefields, enter the names of the Scenes you would like to switch between,My Second Sceneandscene1.scene2 -
Selectby double-clicking it in the Project, and press
scene1. ThePlayscene will appear.scene1 -
Click thebutton and
Load Next Scenewill be loaded.scene2
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 . To make this example work:
testScene-
Create a new Project.
-
Change the name of the default scene toby selecting it and then use Assets->Rename.
testScene -
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").
-
Create a C# Script and call it /Example.cs/.
-
Add the following script text to /Example.cs/.
-
Next add an empty GameObject calledto each of the two scenes.
GameObject -
Finally copy /Example.cs/ to each of the two GameObjects.
Use the button to launch the scene. A GUI Button is shown which allows the scenes to swap.
GametestSceneusing UnityEngine;using UnityEngine.SceneManagement;// SceneManagement.SceneManager-name examplepublic 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"); } } }}