0

Hey I am making a gameover screen for my game. I want it to get gameover screen, then when player dies it restarts that level. I made it in a inefficient way making multiple gameover screens then connecting each gameover to their scene so it could restart that scene. My problem is that it still goes to my main level and skips tutorial. Code:

    if (health == 0)
    {

        if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("Level 2 - Damages"))

            SceneManager.LoadScene("Gameover 1");

        if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("Level 3 - Wall Climbing"))

            SceneManager.LoadScene("Gameover 2");

        if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("Level 4 - FallingPlatform"))

            SceneManager.LoadScene("Gameover 3");

        if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("Level 5 - Buttons And Switches"))

            SceneManager.LoadScene("Gameover 4");

        if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("Final Level"))

            SceneManager.LoadScene("Gameover");


    }
2
  • If I understand, you probably just want to store the scene that the player died in, like in PlayerPrefs, then look it up from whatever mechanism you have in the game over scene to restart.
    – Crowcoder
    Commented Dec 5, 2018 at 12:01
  • Why don't you store the current scene to a static int or string somewhere, then when you hit your gameover scene just reload that stored value? You could also use PlayerPrefs to do it.
    – AresCaelum
    Commented Dec 5, 2018 at 15:18

1 Answer 1

1

i dont really understand what your correlation between gameover scenes and your main/tutroial scene is but..

In the Build Settings, from the File menu, you can reorder your scenes. Make sure startup/tutorial/whatever scene you want to start with, is at the top of the list.

update; ps. a switch is probably easier as well something along the lines of:

 if (health == 0)
    {
      switch(SceneManager.GetActiveScene()){
       case SceneManager.GetSceneByName("Level 2 - Damages"):
            SceneManager.LoadScene("Gameover 1");
            break;
       case default: 
             SceneManager.LoadScene("MainMenu");
        }
    } 
4
  • my tutorials are scenes that the player goes through, so if the player dies in the tutorial my bug is that it goes to gameover then to final level no matter if it was the first tutorial Commented Dec 5, 2018 at 12:02
  • Where do you check if it is the tutorial when health is 0. if you end the scene but do not load a new one, shouldnt that cause the game to jump to the next scene in the scene order? maybe add an if where you can check wether its the tutorial or not. PS. using a switch is probably easier.
    – Daan
    Commented Dec 5, 2018 at 12:07
  • added it into my answer.
    – Daan
    Commented Dec 5, 2018 at 12:10
  • I fixed it by just setting the gameover scene after each tutorial then +1 if player dies and -1 when restart is pressed. After that if player goes through i just +2 Commented Dec 5, 2018 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.