Version: 2017.3 (switch to 2017.4)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Scene.name

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public var name: string;
public string name;

Description

Returns the name of the scene.

no example available in JavaScript
//This script changes the Scene depending on Button clicks and the Scene’s name

//To make this work, attach this script to a GameObject //Click on the GameObject and go to the Inspector. Go to the "My First Scene" field and "My Second Scene" field and type in the name of your Scenes you would like to switch between. //Create a Button (Create>UI>Button) and attach this in the "My Button" field of the Inspector. //Place a Object.DontDestroyOnLoad script on your Canvas to ensure the Button isn’t deleted. You may want to add to this to remove copies on a second load. //Make sure both of your Scenes have an EventSystem as a child of your Canvas (Create>UI>Event System).

using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI;

public class SceneManagementSceneNameExample : MonoBehaviour { //These are your Scene names. Make sure to set them in the Inspector window public string m_MyFirstScene, m_MySecondScene; Scene m_Scene; public Button m_MyButton;

void Awake() { //Ensure the script is not deleted while loading DontDestroyOnLoad(this);

//Make sure there are copies are not made of the GameObject when it isn't destroyed if (FindObjectsOfType(GetType()).Length > 1) //Destroy any copies Destroy(gameObject);

//Call the MyButton function when you click the Button m_MyButton.onClick.AddListener(MyButton); }

void Update() { //Return the current Active Scene in order to get the current Scene's name m_Scene = SceneManager.GetActiveScene();

//Check if the current Active Scene's name is your first Scene if (m_Scene.name == m_MyFirstScene) //Change the Text of the Button to say "Load Next Scene" m_MyButton.GetComponentInChildren<Text>().text = "Load Next Scene"; //Otherwise change the Text to "Load Previous Scene" else m_MyButton.GetComponentInChildren<Text>().text = "Load Previous Scene"; }

void MyButton() { //Check if the current Active Scene's name is your first Scene if (m_Scene.name == m_MyFirstScene) { //Load your second Scene SceneManager.LoadScene(m_MySecondScene); }

//Check if the current Active Scene's name is your second Scene's name if (m_Scene.name == m_MySecondScene) { //Load your first Scene SceneManager.LoadScene(m_MyFirstScene); } } }

Did you find this page useful? Please give it a rating: