In previous versions of unity I could make a function with application.LoadLevel("sceneName’) and the equip it to whatever I want to change the scene. Now I have to use SceneManager. I looked on the documentation, and its not much help.
public static void LoadScene(int sceneBuildIndex, SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);
public static void LoadScene(string sceneName, SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);
This is the example they give, so I though well, Ill just put that in my script and use it. But it just had tones of errors. I tried reading about it, but I couldn’t get anywhere.
I would appreciate it if someone could make a quick reference script and explain how it works. Thanks so much!
Hey for anyone looking at this question because they are having trouble changing the scene.
Here is the answer!
So first if you are changing scenes, you obviously have two scenes.
Open up the scene you want to have the button in to change to the other scene. Place a canvas , and then a button inside. Then place a gameObject anywhere in the scene and name it buttonPressed, or whatever you want. Now you can write your change scene script and stick it into that gameObject for later.
You have to add using UnityEngine.SceneManagement;
Then define your function in which you want to change the scene. Name it ManageScene. Lastly, inside that function, you can change the scene with sceneManager.LoadScene(“SceneName”);
You can put either the scene name, or the scene index in the loadScene parameter.
The script should look like this:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class ChangeScene : MonoBehaviour {
public void ManageScene(){
SceneManager.LoadScene ("SceneToChangeTo");
}
}
Now once you are back in the editor, open up “File” and then “Build Settings” and drag your two scenes into the Build in scene section. Then you can see the scene index to the left if you want to use that instead of the scene name.
Drag your script into that buttonPressed gameOgject. Click on the button you are using to change the scene. Click the + to add a new onClick event. Drag the buttonPressed gameObject into the field that says None(object). Then click on NoFunction and click on your script “changeScene”. The last thing we need to do is select the function you made in the selection that pops up. I called it “ManageScene”. Now you should have a working scene changer button.
It looks like @Payton-Dugas is on to something.
I have a lightweight OverallScene to start things off
Scene itself is now an object, and you can get this object during the Awake() function.
I have a public myScene in all my main Singleton objects
Then on the Update Event, I then set up my next scene using SceneManager.LoadSceneAsync(scenename) and use the resulting AsyncOperation object to then check on the progress and the isDone parameter
Once isDone is true. You are good to use allowSceneActivation to true and should load your scene.
Otherwise you risk it blowing up… I believe.
I’m actually trying this stuff out as I write here. So, my word is not gospel. But hopefully leads to a better answer.
Edit: Well, the isDone parameter doesn’t appear to ever get set…
AsyncOperation.progress goes to 0.9F and stays there. But it does start at a smaller number and get bigger. So I think that it is a good (enough) indicator for me to say that the scene is loaded and proceed with the other game functions in the newly loaded scene.
Also, I’m checking all my singletons on the Awake and make sure they don’t do anything until my “safety check” is completed.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class ChangeScene : MonoBehaviour
{
public void ManageScene(int level)
{
SceneManager.LoadScene (level);
}
}
so, in the “On Click” fuction of the button you can input the level of the scene: 0 or 1 or 2… so you don’t have to rewrite the script each time you add a level or modify it.
if you want to input a name level instead of a number level just replace (int level) by (string level)