Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 844
Johny Avatar asked Sep 06 '26 08:09

Johny


2 Answers

When you go BackToLastScene you set currentScene = GetLastScene(); but never change lastScene value.

I suggest you use some sort of LIFO data structure like Stack from System.Collections to keep track of the scenes.

Here's a sample pseudo-code:

define scene_stack;
function LoadNewScene(new_scene){
    current_scene = GetCurrentScene();
    scene_stack.Push(current_scene);
    LoadScene(new_scene);
}

function LoadOldScene(){
    old_scene = scene_stack.Pop();
    LoadScene(old_scene);
}
like image 160
Damiano Caprari Avatar answered Sep 08 '26 00:09

Damiano Caprari


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;
    }    

}
like image 43
Johny Avatar answered Sep 07 '26 23:09

Johny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!