Search code examples
androidunity-game-enginebackscene

Unity - Back to last scene using Android Device Back Button


In Unity, I manage to go back to the last scene by clicking the Device Android back button but the problem is when I am in the 3rd scene and I want to go back to 2nd and then back to 1st scene.

From 3rd to 2nd scene, when I click back button it goes successfully but when I press the back button again from 2nd scene to 1st scene it doesn't go. It looks like that the back button works only for the first time.

Here is my c# code:

public class SceneLoader : MonoBehaviour
{
    private float seconds = 0.5f;
    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(seconds);
        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();
            Debug.Log("Now the Current scene is: " + currentScene);    
        }
    }


    public static void SetLastScene(int currentSceneToLastScene)
    {
        lastScene = currentSceneToLastScene;
    }

    public static int GetLastScene()
    {
        return lastScene;
    }

    public void QuitGame()
    {
        Application.Quit();
    }

}

Thanks in advance.


Solution

  • After the help from @Damiano, I changed the my code like this and it works:

    public class SceneLoader : MonoBehaviour
    {
        private float secondsToLoadNextScene = 0.5f;
        private static int lastScene;
        private int mainScene = 1;
        private int currentScene;
    
        public static Stack<int> sceneStack = new Stack<int>();
    
    
        private void Awake()
        {
            currentScene = SceneManager.GetActiveScene().buildIndex;
        }
    
        private void Update()
        {
            BackButtonPressed();
        }
    
        public void LoadNextScene(int numberOfSceneToLoad)
        {
            StartCoroutine(LoadScene(numberOfSceneToLoad));
        }
    
        private IEnumerator LoadScene(int numberOfScene)
        {
            SetLastScene(currentScene);
    
            yield return new WaitForSeconds(secondsToLoadNextScene);
            LoadNewScene(numberOfScene);
        }
    
        public void BackButtonPressed()
        {
            if (Input.GetKey(KeyCode.Escape) && currentScene > mainScene)
            {
                if (lastScene == 0)
                {
                    Debug.Log("Last scene was Splash Screen so load Main Scene instead.");
                    SceneManager.LoadScene(mainScene);
                }
                else
                {
                   LoadLastScene();                    
                }         
            }
        }
    
        public void LoadNewScene(int sceneToLoad)
        {
            currentScene = SceneManager.GetActiveScene().buildIndex;
            sceneStack.Push(currentScene);
            SceneManager.LoadScene(sceneToLoad);
        }
    
        public void LoadLastScene()
        {
            lastScene = sceneStack.Pop();
            SceneManager.LoadScene(lastScene);
        }
    
        public static void SetLastScene(int makeCurrentSceneTheLastScene)
        {
            lastScene = makeCurrentSceneTheLastScene;
        }
    
        public static int GetLastScene()
        {
            return lastScene;
        }    
    
    }