Referenced the documentation for SceneManager but still confused how to rewrite Application.loadedLevel and Application.LoadLevel using the new SceneManager class. Can anyone help?
I forgot what Application.loadedLevel used to return.
You can use this now
UnityEngine.SceneManagement.SceneManager.GetActiveScene ();
If Application.loadedLevel returned a string, you can do this
UnityEngine.SceneManagement.SceneManager.GetActiveScene ().name;
1 Like
Thanks. Almost there. Say I have just 2 scenes with the identical script in each
With the following script I can go to Scene 2 from Scene 1 but I can’t go back. I’ve correctly defined sceneToLoad in each scene to match the value in Build Settings. ie 0 and 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class TraverseSceneScript : MonoBehaviour {
public int sceneToLoad;
void OnGUI(){
GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height - 80, 100, 30), "Current Scene: " + (SceneManager.GetActiveScene().buildIndex - 1));
if(GUI.Button(new Rect(Screen.width/2 - 50, Screen.height -50,100,40), "Load Scene " +(sceneToLoad +1 ))){
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex+sceneToLoad);
}
}
}
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex+sceneToLoad);
If sceneToLoad holds the build index of the scene you want to load, there’s really no reason to fetch the current scene’s index and add to it.
This should work just fine
SceneManager.LoadScene(sceneToLoad);
1 Like
Thanks!