Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Simple Question. How do you Load the levels you want?

Discussion in 'Scripting' started by solider, Jun 16, 2015.

  1. solider

    solider

    Joined:
    May 28, 2015
    Posts:
    9
    So I am making a 2d game. I wanted to player to have two lives for each level. After the player loses two lives, I want the next death to load up the game over scene. So I understand how to loadlevels with Application.LoadLevel(int). But I do not understand how reload the same scene over again and when it reloads the third time, when the player dies, it reloads to the game over screen. What usually happens, my code loads the same scene again and again and never hit the gameover screen, because the counter resets after each scene reload.
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    maybe you can use Application.LoadLevel(Application.loadedLevel);
    it should reload your current scene. This way you dont need to hardcode in your levels.

    You can keep a manager of how many times the player died and use dontdestroyonload with it or just make it a static variable. Before you reload the scene you check if the player still has lives, if not, load the gameover.
     
  3. solider

    solider

    Joined:
    May 28, 2015
    Posts:
    9
    ok thanks
     
  4. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    you can just use

    Code (CSHARP):
    1. Application.LoadLevel("NAME OF SCENE YOU WANT TO LOAD");
    ps : the scene must be added to the build settings