Search Unity

What level I came from...

Discussion in 'Scripting' started by klindeman, Aug 21, 2005.

  1. klindeman

    klindeman

    Joined:
    Jun 13, 2005
    Posts:
    295
    Is it possible to tell what level I came from when a level loads?

    On my High Score screen I need to know if I came from the main menu selection or from the end of my level.
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Two possible solutions:

    1) Store it in a static variable before loading the level

    2) Store it in a dedicated behaviour for this purpose eg. a game manager, call DontDestroyOnLoad (this); on the behaviour when starting up your game.
    DontDestroyOnLoad will make the whole game object never be destroyed automatically on level load.
     
  3. klindeman

    klindeman

    Joined:
    Jun 13, 2005
    Posts:
    295
    Ok, I was thinking about those two. I was just curious if there was a built in solution.
     
  4. klindeman

    klindeman

    Joined:
    Jun 13, 2005
    Posts:
    295
    Ok, I ended up doing number 2 (mostly cause I was not sure how to approach number 1, and I knew how to do number 2 somewhat), and was putting the DontDestroyOnLoad() in the Awake(). I think this might be the wrong place to do it? It works fine until I get to the High Score screen, and want to go to the main menu, because then another one appears from when it loads the scene again (The Object I am using DontDestroyOnLoad() on was originally created in the Main Menu scene).

    Another way I could do this, is create a scene that sets up all of the ones I don't want to destroy on load, and then have it automatically load the Main menu, and not load it again. That seems kind of like a hack way to do it though...
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    A pattern we used for gooball is having a single game loader scene.
    This contains the entire gameflow, basically a game controller.
    This scene is then never loaded again and all of its game objects are made DontDestroyOnLoad.

    We also put the startup screen in that game loader scene, and that obviously will want to be destroyed after you have shown a logo etc.
     
  6. klindeman

    klindeman

    Joined:
    Jun 13, 2005
    Posts:
    295
    Ok, so maybe that isn't as much of a hack as I thought it was!