How I solved Challenge, is this good code?

private void LoadNextScene()
{
    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;        
    if (currentSceneIndex < 2)
    {
        SceneManager.LoadScene(currentSceneIndex + 1);
    }
    else
    {
        SceneManager.LoadScene(0);
    }
}
1 Like

Hello Christopher,

I’m sure the logic is sound, two observations;

  1. if any of the other methods in your LevelManager also run off and get the build index of the current scene for any of their tasks you could consider making the currentSceneIndex a member variable, e.g. a variable at the class level, and then populate it each time a new scene loads. If it isn’t used anywhere else, then there’s no real need to do this.

  2. currentSceneIndex <2 and LoadScene(0) look like magic numbers, if I had to guess, the 2 indicates that you have a limited number of scenes at the moment and thus you are trying to contain the increment within those boundaries, as for scene zero, maybe a start / menu scene? I wouldn’t be able to tell without looking at the scenes specifically. Nothing massively wrong with this, but you could either drop a comment on the end, e.g.

SceneManager.LoadScene(0);    // load the first level

or, if you have special scenes, such as a main menu, win, lose etc, consider using an enum to represent them, for example;

public enum SpecialScenes { MainMenu = 0, Win = 1, Lose = 2 };

…then when you are going to bounce the player back to the main menu for example, the code may look like this;

SceneManager.LoadScene((int)SpecialScenes.MainMenu);

In a small game, a comment would probably be easier, but in both cases it just adds a little bit of additional readability, handy when you come back to this project in a years time and can’t remember what the numbers represent.

If the 2 is a boundary, another approach is to place all of the special scenes, e.g. main menu, win, lose etc at the beginning, then you can rest assured that any scenes after these are playable levels. Instead of then using a hard-coded value, you can use SceneManager.sceneCountInBuildSettings to get the maximum number. This approach would allow you to add another couple of levels and not have to remember to come back an update the number 2 for example.

Using your code as an example and slotting the above in, would look like this;

private void LoadNextScene()
{
    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;        
    if (currentSceneIndex < SceneManager.sceneCountInBuildSettings -1)
    {
        SceneManager.LoadScene(currentSceneIndex + 1);
    }
    else
    {
        SceneManager.LoadScene(0);
    }
}

The minus 1 is because the scenes are zero-based, e.g. the first scene is index 0, but the count is the number of scenes etc.

Hope this helps :slight_smile:


See also;

1 Like

Thanks for the detailed reply! The magic number explanation did come to mind, so I’m glad I’m not completely lost here. I’m totally new to programming so thank you for the advice :slight_smile:

1 Like

You’re very welcome.

Well done for creating your solution which obviously works, the above are just suggestions for going forward which may help you dodge issues. :slight_smile:

Privacy & Terms