Search Unity

How to load a previous scene

Discussion in 'Editor & General Support' started by Icarus_07, Jul 8, 2017.

  1. Icarus_07

    Icarus_07

    Joined:
    Jul 7, 2017
    Posts:
    6
    So this is probably a complete noob question to ask but I've been stuck on this for a while now and none of the other threads are helping.

    I have a short game with 3 actual levels and a bonus level, and some UI stuff as scenes. After the player finishes the game (the second to last scene since the bonus level is on the very bottom of the build index), I want there to be a button that says "Back to Main Menu" where it, of course, takes them back to the main menu (first scene). Originally I thought I could just use the regular scene loading script where I tell the build settings to add 1 to whatever scene it is on, then load that scene, but instead of + 1, I used -5 and it didn't work.

    If anyone wants to see what my code looks like as of right now just ask for it and ill get back asap.

    I've been coding for literally only 3 days now and a lot of times coding vocabulary just goes straight over my head, so be patient with me and try to explain roughly what something does (if it isn't too much to ask for)
     
  2. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    It's a lot easier to load the scene by name rather than index. SceneManager.LoadScene will take a string instead of the index value if you want to use it that way.
     
  3. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    If the scene you want to load is scene 0 (the first) don't use any +1 or -5, simply use 0 or 1 or what ever the scene index is.
     
  4. Icarus_07

    Icarus_07

    Joined:
    Jul 7, 2017
    Posts:
    6
    Alright, so how do I actually type that out into code then? would it be:

    public void LoadNextLevel()
    {
    SceneManager.LoadScene(0)
    }
     
  5. Icarus_07

    Icarus_07

    Joined:
    Jul 7, 2017
    Posts:
    6
    I just tried that bit of code now and it worked! Thank you two very much!
     
    larku likes this.