- Home /
Android device Back button not going back to last scene
In Unity, I am trying to go back to the last scene by clicking the Device Android back button and not back to the main screen.
I tried to save the state of the last scene but I always get that the last scene is 0 instead of the correct scene number.
Here is my c# code:
public class SceneLoader : MonoBehaviour {
private static int lastScene;
private void Update()
{
if (Input.GetKey(KeyCode.Escape))
{
Debug.Log("Last scene: "+ GetLastScene());
SceneManager.LoadScene(GetLastScene());
return;
}
}
public void LoadNextScene(int numberOfScene)
{
StartCoroutine(LoadAfterSeconds(numberOfScene));
}
private IEnumerator LoadAfterSeconds(int numberOfScene)
{
yield return new WaitForSeconds(1f);
SceneManager.LoadScene(numberOfScene);
SetLastScene(numberOfScene);
gameObject.SetActive(true);
}
public static void SetLastScene(int level)
{
lastScene = level;
}
public static int GetLastScene()
{
return lastScene;
}
}
If I don't call SetLastScene at all, then lastScene is always 0 and so when I press the Android device back button or "Escape" it goes to main screen (number 0). But when I use SetLastScreen it always get the current scene number instead of the previous scene.
Thank you.
Answer by Gng357 · Nov 29, 2018 at 02:36 PM
I have tested the code @HenBOMB but the SceneManagement.LoadLevel doesn't seem to work for me.
I have changed my code and now it works:
public class SceneLoader : MonoBehaviour
{
private static int lastScene;
private int currentScene;
private void Awake()
{
currentScene = SceneManager.GetActiveScene().buildIndex;
}
private void Update()
{
BackButtonPressed();
}
public void LoadNextScene(int numberOfSceneToLoad)
{
StartCoroutine(LoadScene(numberOfSceneToLoad));
}
private IEnumerator LoadScene(int numberOfScene)
{
currentScene = SceneManager.GetActiveScene().buildIndex;
SetLastScene(currentScene);
yield return new WaitForSeconds(0.5f);
SceneManager.LoadScene(numberOfScene);
}
private void BackButtonPressed()
{
if (Input.GetKey(KeyCode.Escape))
{
Debug.Log("Current scene: " + currentScene);
Debug.Log("Last Scene (scene to load): " + GetLastScene());
SceneManager.LoadScene(GetLastScene());
currentScene = GetLastScene();
lastScene -= lastScene;
Debug.Log("Now the Current scene is: " + currentScene);
Debug.Log("And the scene to load is: " + lastScene);
}
}
public static void SetLastScene(int currentSceneToLastScene)
{
lastScene = currentSceneToLastScene;
}
public static int GetLastScene()
{
return lastScene;
}
public void QuitGame()
{
Application.Quit();
}
}
Answer by HenBOMB · Nov 28, 2018 at 11:08 PM
I think its because you never assigned the value of your 'lastScene', so it sets it as default 0. the script was kind of condusing to interpret so heres what i wrote:
int currentScene;
void Update(){
if(Input.GetKeyDown(KeyCode.Escape)){
StartCoroutine(LoadPreviousScene(1));
}
}
IEnumerator LoadNextScene(float waitingTime){
yield return new WaitForSeconds(waitingTime);
currentScene++;
SceneManagement.LoadLevel(currentScene);
}
IEnumerator LoadPreviousScene(float waitingTime){
yield return new WaitForSeconds(waitingTime);
currentScene--;
SceneManagement.LoadLevel(currentScene);
}
int GetCurrentScene(){
return current;
}
int GetNextScene(){
return current++;
}
int GetPreviousScene(){
return current--;
}
Your answer
Follow this Question
Related Questions
Back button 3 Answers
Reloading Scene - Android/Quest 1 Answer
Android back button / Pause and exit game 0 Answers
Android back button not working 2 Answers
App crashes after launching scene with AR Core for second time. 1 Answer