Search code examples
c#unity-game-enginescene-manager

checkpoints c# in unity


i am making a game with c# in unity and i want that if you die in level 4 that you go back to level 0 but if you are in level 5 that you respawn in level 5 the code:

   {
        int currentSceenIndex = SceneManager.GetActiveScene().buildIndex;
        if (currentSceenIndex <= 4)
        {
            SceneManager.LoadScene(0);
        }
        else if (currentSceenIndex == 4)
        {
            SceneManager.LoadScene(4);
        }   
    }

what did i do wrong the respawn still works but i am always going back to level 0 how to fix this. I tried to follow an tutorial but it still did not work. I can't see the mistake and also I don't get any errors when playing the game so I don't know where the error is at.


Solution

  • It sounds like you would rather want to use

    if (currentSceenIndex < 4) 
    

    instead of <= since currently the second block would never be reached since for currentSceenIndex == 4 it already executes the if block

    var currentSceenIndex = SceneManager.GetActiveScene().buildIndex;
    if (currentSceenIndex < 4)
    {
        SceneManager.LoadScene(0);
    }
    else if (currentSceenIndex == 4)
    {
        SceneManager.LoadScene(4);
    }