Select your preferred scripting language. All code snippets will be displayed in this language.
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.
CloseFor 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.
CloseReturns 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:
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:
Thanks for helping to make the Unity documentation better!