- Home /
Unity script to open a new scene
Hello, I'm trying to open a scene using this script. But every time I click the button, it just quits the game.
function OnGUI () {
if (GUI.Button (Rect (10,120,150,100), "Play Game")) {
EditorApplication.OpenScene("Assets/CharacterSelection.unity");
}
}
Answer by Jean-Fabre · Jan 27, 2011 at 05:05 AM
Hi,
Are you trying to build an authoring tool? or is it within your game? If you want to open a scene within your game, then you should use
Application.LoadLevel("characterSelection");
Bye,
Jean
Thank you for this! Can I say:
1) LoadLevel can either take a String or a number 2) You must add both scenes into the build settings
Answer by Justin Warner · Jan 27, 2011 at 05:04 AM
http://unity3d.com/support/documentation/ScriptReference/Application.LoadLevel.html
Enjoy and have a good night!
I think this link is dead. Here's the new one: Application.LoadLevel
Answer by pankajshivalkar · Nov 27, 2018 at 02:26 PM
If you are currently working with scenes then use SceneManager.LoadScene(string);
as Application.LoadLevel(string);
is Deprecated.
The usage is as follows:
using UnityEngine;
using UnityEngine.SceneManagement;
public class ExampleClass : MonoBehaviour
{
void Start()
{
// Only specifying the sceneName or sceneBuildIndex will load the Scene with the Single mode
SceneManager.LoadScene("OtherSceneName", LoadSceneMode.Additive);
}
}