How can I programmatically change the default scene in Unity?
Currently, I'm only able to change the default scene via the Unity editor...
I want to change default scene so that the next time the user enters the app it load another scene.
Any help is appreciated.
You can set, as the initial scene, an empty one, that just loads another scene (based on a previously saved value) as soon as it starts. As an example, you can retrieve this value using PlayerPrefs, as it:
void Start(){
int defaultLevel = PlayerPrefs.GetInt("defaultLevel");
Application.LoadLevel(defaultLevel);
}
SceneManager.LoadScene(0); //Use scene index
SceneManager.LoadScene("sceneName"); //Use scene name
http://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html
Make sure to add the scenes to the Build Settings before trying the code above.
Related
I have a audio player during scene 0 in my game (the menu). I have a code that intends to prevent the objects destruction when changing scenes. This works.. mostly. But for some reason, when moving to scene 1 a second audio player is created. This does not happen when moving to scene 2, 3, etc. I cannot find any components in scene 1 indicating a second audio player.
I have attached screen shots that may be of assistance. Any help with this would be greatly appreciated. I am stumped.
audio player and code ....
duplicated audio in scene 1....
In your Scene Changing Script, Create a New Function (or edit existing function)
LoadSceneWithoutAudioDestruction or Whatever seems good to you
Then Write down the Following Code.
public GameObject AudioSouce; //in starting of file to reference Audio
public void LoadSceneWithoutAudioDestruction(){
DontDestroyOnLoad(AudioSouce);
SceneManager.LoadScene(SceneNameasString);
}
Now on button which you want to Load next Scene attach this Script and ad function LoadSceneWithoutAudioDestruction to On Button click Event of that button.
Also Ensure that the next scene that you are Loading does not habe any Audio Source Already
If any Problem exists, ask me in comment.
So I’m making a game and I made a Store in it, and I need a script to put in a button in the store scene, that go to the previous loaded scene. For example, I completed the level one and now I’m in the level selection menu, I want to go to the store so I click in “store” button. Now, in the store, I want to go back, so I click in the “back” button and instead of going back to the level selection menu, I go to the start menu, so I need a script to put on the “back” button that brings me to the previous loaded scene. Can you help me?
put this in any class
static List<string> sceneList = new List<string>();
when you load a new scene , right before you call sceneManager
<classyouputscenelistin>.sceneList.Add("<scenename>");
when you go back
SceneManager.LoadScene(<classyouputscenelistin>.sceneList[<classyouputscenelistin.sceneList.Count -2], LoadSceneMode.Single);
replace to the class name you added scene list to
replace with the name of the scene you are loading
edit: go back to scene before last
SceneManager.LoadScene(<classyouputscenelistin>.sceneList[<classyouputscenelistin.sceneList.Count -3], LoadSceneMode.Single);
Basically Count - 2 is previous scene anything before that (-3, -4) load the scenes before it, if you don't want a scene to be tracked in this list don't do this:
<classyouputscenelistin>.sceneList.Add("<scenename>");
I have a game where I need to save a scene. I'm doing this by setting the scene to a variable which I can access later. The player then goes to a main menu when they are done.
The player now wants to go back to that scene, however, when it loads normally, it resets to its original state. Is there any way to use this variable to set the scene's contents to the variable I made earlier?
This is how I made the variable: Builder1 = SceneManager.GetActiveScene();
Any help would be greatly appreciated (and yes, I do realize that the variable I made probably doesn't store the scene the way I want it to). Thanks!
Scene is just a reference to the Scene object, and not all of the GameObject's locations/information at that moment in time. If the information in a scene shouldn't be lost yet, then it is advisable to not close the scene yet. Instead, take a look at Additive Scene Loading. You can addivitely load your new scene, switch your main camera to the new scene's camera, and when you are done there you can close the added scene and return the camera to the original scene.
If you're looking to persist data between game loads, then you'll need to consider serializing all of the relevant data in your scene and build a data class to house all of it.
I have a scene that I'm working on using Steam VR 2.0, and Unity 2018.3.2f1. I have a simple statement in it that reloads the scene
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadSceneAsync("Final");
}
}
The issue is: when I reload the scene, it stops responding properly. I am still able to move my head around, and hover over objects. And the objects I hover over get highlighted, but they stay highlighted. I'm not able to pick them up, or interact with them in any meaningful way, and I don't know why this is happening.
I've attached a screenshot of the issue below.
As you can see, multiple objects are highlighted, and the hand mesh is weird:
Solutions I've tried--
Using LoadScene instead of LoadSceneAsync
Using Application.LoadScene instead
Tried to edit the Player script in SteamVR library to not add it to Don't Destroy On Load
Any suggestions?
The issue was arising because the Player prefab in SteamVR 2.0 had Do Not Destroy on Load checked. So, there were multiple players being instanced when I reloaded the scene. I unchecked that box, and now everything is in order.
The checkbox is located inside the [SteamVR] object under the Player prefab:
I have two scenes: Menu and Game. I'm using C#.
When you die in the game, you get sent back to the menu with
SceneManager.LoadScene("Menu");
There's a script with a GUIButton in the menu that, when clicked, loads the game with
SceneManager.LoadScene("Game");
What I want to happen is that when I click the button in the menu scene, it loads the game as if I just clicked the play button with the game scene open in the editor.
What instead happens is that it goes to the game scene, but some objects from the game scene appear to be missing. I'm not using DontDestroyOnLoad() anywhere.
Some objects from the scene do appear, but others don't. The weirdest thing is that there are some data fields on a script on the missing object that are referred to by some other scripts, and those give values that make sense.
Does anyone have any idea what's going on, or what I can do to get the desired result?
Turns out that the problem was that I was destroying one of my objects if it was not the first time that object was created, and I didn't realize it.
are those static objects or dynamic? try to instantiate them in function which gets called on scene loaded.