Lior Elisberg
Lior Elisberg

Reputation: 61

Why does my Enum parameter goes back to default value?


I have created 4 levels, but I'm stuck in the first two because my enum Level keeps going back to default=level_1. So I'm stuck between levels 1 and 2. Why does my parameters go back to initial/default value?

This is true as well for any other parameters (int) I've tried.

I have already refreshed the scenes in the build setting.

//the enum parameter I use

enum Level { Level_1, Level_2, Level_3, Level_4 };

Level currentLevel = Level.Level_1;

//the loading next scene function

private void LaodNextScene()
{
    switch (currentLevel)
    {
        case Level.Level_1:
            {
                print("loading level 2");
                currentLevel = Level.Level_2;
                SceneManager.LoadScene("Level_2");
                break;
            }
        case Level.Level_2:
            {
                print("loading level 3");
                currentLevel = Level.Level_3;
                SceneManager.LoadScene("level_3");
                break;
            }
        case Level.Level_3:
            {
                print("loading level 4");
                currentLevel = Level.Level_4;
                SceneManager.LoadScene("level_4");
                break;
            }
        case Level.Level_4:
            {
                print("loading level 4 again");
                SceneManager.LoadScene("level_4");
                break;
            }
        default:
            print("invalid level");
            break;
    }
}

//the loading first scene function

private void LaodFirstScene()
{
    currentLevel = Level.Level_1;
    SceneManager.LoadScene(currentLevel.ToString());
}

I expect to be able to move forward in my levels, but I'm stuck in the first two:

When I die - level 1 restarts.

When I win - level 1 goes to level 2, however level 2 restarts itself.

Upvotes: 2

Views: 158

Answers (1)

rrswa
rrswa

Reputation: 1050

Within the switch statement you set currentLevel to the next level, then you load a new scene. I suspect that when the new scene loads currentLevel = Level.Level_1 is called again and resets currentLevel

You can try and replace that line to something like currentLevel = (Level)SceneManager.GetActiveScene().buildIndex;

or you can make a Game Manager that keeps track of the current level with the singleton approach. It's a bit more complicated but probably a more elegant solution.

If you want to know more about singletons:

Upvotes: 3

Related Questions