Alternate method for play again button

So I like to challenge myself as a long time web developer to come up with solutions on the challenge and I actually came up with a different method of handling the play again button on scene 3 that allows you to add an infinite amount of scenes and never have to change the button click function of LoadNextScene().

public void LoadNextScene()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

    if (currentSceneIndex+1 == SceneManager.sceneCountInBuildSettings)
    {
        SceneManager.LoadScene(0);
    }
    else
    {
        SceneManager.LoadScene(currentSceneIndex + 1);
    }
}

So in the text above you will notice I added an if else, the if checks currentSceneIndex+1 (because scene index starts from 0, against sceneCountInBuildSettings, which returns a number for the amount of scenes you have added to build settings, in this case 3.

So when we are on scene index 2, we add 1 to compare it to scene count, and if it matches, we go back to first scene using SceneManager.LoadScene(0);

And of course, if currentSceneIndex does not match, then we simply go to currentSceneIndex+1, the same as we did on scene 0 and scene 1.

Just thought Id toss this up here to show that experimenting can be useful to learn things as well during the challenges.

Also, its good to listen to all the hints on the challenges before pausing the video and attempting them, because I had not done that I didnt catch the hint about creating another public method called LoadFirstScene()

Privacy & Terms