Ok, so I'm working on a small project that has a main menu and 10 levels. From time to time I edit different levels, and want to try them out, however I get a NullPointerException
as my levels rely on certain variables from the main menu for the levels to work, which means I have to alter my levels, then load my main menu and play from there.
Is there something that can be done in the Unity Editor to default load a specific scene when you hit Play, and not the scene you're on?
I could obviously resolve this to something like
public bool goToMenu; //set this to true in my levels through inspector
Start()
{
if (goToMenu)
//then load main menu
}
but it would be really handy if there was a way to set the default level to load when hitting play mode. I've had a look in Preferences but couldn't find anything.
Thanks!
How to Load and Save Your Scene in Unity. Double-click the scene asset in your project window if you want to load or open a scene. Then, go to File and then New Scene from the menu that appears. From your menu, go to File > Open Recent Scene, and then The File Name.
Open the New Scene dialog (menu: File > New Scene or Ctrl/Cmd + n). Select a template from the list. If you want Unity to load the new Scene additively (see note below), enable Load Additively.
Use LoadSceneMode to choose what type of Scene loads when using SceneManager. LoadScene. The available modes are Single and Additive. Single mode loads a standard Unity Scene which then appears on its own in the Hierarchy window. Additive loads a Scene which appears in the Hierarchy window while another is active.
Click and hold the right mouse button. Move the view around using the mouse, the WASD keys to move left/right/forward/backward, and the Q and E keys to move up and down. Hold down Shift to move faster.
I made this simple script that loads the scene at index 0 in the build settings when you push Play. I hope someone find it useful.
It detects when the play button is push and load the scene. Then, everything comes back to normal.
Oh! And it automatically executes itself after opening Unity and after compile scripts, so never mind about execute it. Simply put it on a Editor folder and it works.
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.SceneManagement;
[InitializeOnLoadAttribute]
public static class DefaultSceneLoader
{
static DefaultSceneLoader(){
EditorApplication.playModeStateChanged += LoadDefaultScene;
}
static void LoadDefaultScene(PlayModeStateChange state){
if (state == PlayModeStateChange.ExitingEditMode) {
EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo ();
}
if (state == PlayModeStateChange.EnteredPlayMode) {
EditorSceneManager.LoadScene (0);
}
}
}
#endif
The easiest way is to set your 0th scene as the default play mode scene:
[InitializeOnLoad]
public class EditorInit
{
static EditorInit()
{
var pathOfFirstScene = EditorBuildSettings.scenes[0].path;
var sceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(pathOfFirstScene);
EditorSceneManager.playModeStartScene = sceneAsset;
Debug.Log(pathOfFirstScene + " was set as default play mode scene");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With