0
\$\begingroup\$

I'm trying to make my first game using unity 3d and I'm trying to make a game over the scene that when you press the try again? button it should take you back to the previous scene, I was thinking of just making it with the code SceneManager.GetActiveScene().buildIndex - 1)); then just place one after every level on the build index so that it takes to the previous level that the player was in, but I think this would just increase the file size (Idk if it actually does), so I was wondering if there is a more efficient manner on how to do this so that I could only have one Game Over Scene in the Build Index?

\$\endgroup\$
1
  • \$\begingroup\$ If you want to add "how you fixed your issue", please create an answer with code and an actual explanation. Just posting an image is not considered as a valid answer, and answering in others posts is not really the way to go either. \$\endgroup\$
    – Vaillancourt
    Commented Mar 12, 2021 at 19:58

1 Answer 1

2
\$\begingroup\$

Presumably you considered storing your scene index just before loading the game over scene, and then returning to the stored index? Something like...

public class GameOver : MonoBehaviour {
    static int _lastSceneIndex;

    public static void GoToGameOverScene() {
        _lastSceneIndex = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene("Game Over");
    }

    public void ReturnToLastScene() {
        SceneManager.LoadScene(_lastSceneIndex);
    }
}

Now code in any other scene can call GameOver.GoToGameOverScene() to transition to the game over scene while remembering the scene it came from. Your try again button can call ReturnToLastScene() to go back.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .