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.
CloseReturn the index of the Scene in the Build Settings.
Scene.buildIndex varies from zero to the number of scenes in the Build Settings
minus one. For example five scenes in the Build Settings
will have an index from zero to four. If an additional Scene
called scene15
is added to the list of five scenes in the Build Settings
it will be given an buildIndex of 5. A scene not added to the Scenes in Build
window will return a buildIndex one more than the highest in the list. A not-added scene called FinalRobots
returns 6 as the Scene.buildIndex.
Returns -1 if the Scene
was loaded through an AssetBundle
.
#pragma strict // Show the buildIndex for the current script. // // The Build Settings window show 5 added scenes. These have buildIndex values from // 0 to 4. Each scene has a version of this script applied. // // In the project create 5 scenes called scene1, scene2, scene3, scene4 and scene5. // In each scene add an empty GameObject and attach this script to it. // // Each scene randomly switches to a different scene when the button is clicked. public class ExampleScript extends MonoBehaviour { function Start() { var scene: Scene = SceneManager.GetActiveScene(); Debug.Log("Active Scene name is: " + scene.name + "\nActive Scene index: " + scene.buildIndex); } function OnGUI() { GUI.skin.button.fontSize = 20; if (GUI.Button(new Rect(10, 80, 180, 60), "Change from scene " + scene.buildIndex)) { var nextSceneIndex: int = Random.Range(0, 4); SceneManager.LoadScene(nextSceneIndex, LoadSceneMode.Single); } } }
using UnityEngine; using UnityEngine.SceneManagement;
// Show the buildIndex for the current script. // // The Build Settings window show 5 added scenes. These have buildIndex values from // 0 to 4. Each scene has a version of this script applied. // // In the project create 5 scenes called scene1, scene2, scene3, scene4 and scene5. // In each scene add an empty GameObject and attach this script to it. // // Each scene randomly switches to a different scene when the button is clicked.
public class ExampleScript : MonoBehaviour { void Start() { Scene scene = SceneManager.GetActiveScene(); Debug.Log("Active Scene name is: " + scene.name + "\nActive Scene index: " + scene.buildIndex); }
void OnGUI() { GUI.skin.button.fontSize = 20;
if (GUI.Button(new Rect(10, 80, 180, 60), "Change from scene " + scene.buildIndex)) { int nextSceneIndex = Random.Range(0, 4); SceneManager.LoadScene(nextSceneIndex, LoadSceneMode.Single); } } }
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!