2
\$\begingroup\$

Im making a 2D game for android and I made over 200 levels/scenes.I also made a game over scene that opens when the players collides with an obscale.In the GameOver scene I added a retry button which I wanna make it so it opens the last level played.Im new in Unity and scripting.I've read other similar questions but none of them fixed my issue.Anyone has any idea that could help ? Im doing this because I don't wanna create over 200 game over scenes and edit each obscale in game.

Thank you.

Edit : Im using javascript.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You have plenty of options, and it really just depends on what your style is and the design of your game.

One of the easier ways to handle this is with a simple scene manager. The first thing you need for scene manager is a script that persists between scenes. This can be done with a method called DontDestroyOnLoad. A very simple scene manager would look something like this (written in C#, because that's what I use, but it should be easy enough to convert):

public class SceneManager: MonoBehaviour {

    static string lastScene;
    static string currentScene;

    void Awake () {
        DontDestroyOnLoad (this.transform.gameObject);
    }

    public static void ChangeScene(string sceneName) {
        lastScene = currentScene;
        currentScene = sceneName;
        Application.LoadLevel(currentScene);
    }

    public static void LoadLastScene() {
        string last = lastScene;
        lastScene = currentScene;
        currentScene = last;
        Application.LoadLevel(currentScene);
    }

}

Next, simply attach your script to an empty game object, and make sure that scene manager game object is created in the first scene. Then, just use the scene manager to handle all your scene changes, and you'll easily be able to go back a level.

\$\endgroup\$
4
  • \$\begingroup\$ Hey,thanks for your fast response but I got some questions : When you say "make sure that scene manager game object is created in the first scene" do you actualy mean the main menu or the first level ? And what exactly do you mean by "just use the scene manager to handle all your scene changes" ? Thank you. \$\endgroup\$
    – Ssiro
    Commented May 31, 2014 at 13:58
  • \$\begingroup\$ Use the ChangeScene method to handle all your scene changes, instead of whatever you're using now. That means load the scene manager before changing any scenes, so I assume before the first level. \$\endgroup\$
    – House
    Commented May 31, 2014 at 14:03
  • \$\begingroup\$ tiny note: You don't have have to write this.transform.gameObject, you could just do this.gameObject I mean, technically that works fine, but it's unnecessary indirection that can sometimes lead to code that says something like this.transform.gameObject.collider.gameObject.tranform (only through several variables, so you don't notice it happening :P) \$\endgroup\$
    – jhocking
    Commented May 31, 2014 at 23:23
  • \$\begingroup\$ @jhocking That's true. The Transform class and the MonoBehaviour class (the this in this instance) are both extensions of the Component class. Which is where the gameObject variable is stored. But you're right, it's unnecessary, and makes the assumption (which is a pretty good assumption, but an assumption nevertheless) that both components are referencing the same gameObject. \$\endgroup\$
    – House
    Commented May 31, 2014 at 23:32

You must log in to answer this question.

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