Void CheckPassword issue

Hey Folks!

New member and C# overall. Seemed to hit a snag, first of many I am sure…

I was trying to implement switches (chapter 27) and run into a bug of sorts.

When I execute Play Game, I can successfully chose level 1 or 2. After inputting the correct password, I proceed to menu which brings me back to the menu. If I chose a different level or attempt to replay the level, or use and invalid input , the game keeps prompting the else in my void CheckPassword.

I have combed through this multiple times but not sure where I am mistaken.

Hi Vinraal,

Welcome to our community! :slight_smile:

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

Hope this helps :slight_smile:


See also;

Hi Nina,

Thanks for your input. I must confess it has been years since touching a forum so bare with me and thanks for being patient.

I did in fact check the lecture changes twice and unfortunately I cannot see where I am fumbling. I usually take my time before reaching out and typically the answer is right under my nose but gosh darnit, I am stuck lol.

void RunMainMenu(string input)
    {
        bool isValidLevelNumber = (input == "1" || input == "2");
        if (isValidLevelNumber)
        {
            level = int.Parse(input);
            StartGame();
        }
        else if (input == "1230") //easter egg
        {
            Terminal.WriteLine("A Lucky Number. However, please choose a valid level...");
        }
        else
        {
            Terminal.WriteLine("Please choose a valid level");
        }
     }

    void StartGame()
    {
        currentScreen = Screen.Password;
        Terminal.ClearScreen();
        switch(level)
        {
            case 1:
                password = level1Passwords[0];
                break;
            case 2:
                password = level2Passwords[1];
                break;
            default:
                Debug.LogError("Invalid Level Number");
                break;
        }
        Terminal.WriteLine("Please enter your password: ");
    }
   
        

    void CheckPassword(string input)
    {
        if (input == password)
        {
            Terminal.WriteLine("Congradulations!!");
        }
        else
        {
            Terminal.WriteLine("Invalid password. Please try again");
        }
    }
}

Appreciate your help and any other input

Thank you. :slight_smile:

Where do you restart your level?

The entry point for your code is OnUserInput, and what’s happening then depends on the if-conditions. The value of state doesn’t automatically change. You have to explicitely set it to the desired value if you want something else to happen. If state is set to Screen.Password, the corresponding if-condition in the OnUserInput method gets evaluated to true.

Did this help?

Hi Nina,
Sorry its been a while, adulting during the apocalypse sure has been a blast… trying to make time here. I can finally focus. Unfortunately I have become a bit rusty since.

I currently have a OnUserInput sting input:

 void OnUserInput(string input)
    {
        if (input == "menu")
        {
            ShowMainMenu();
        }
        else if (currentScreen == Screen.MainMenu)
        {
            RunMainMenu(input);
        }
        else if (currentScreen == Screen.Password)
        {
            CheckPassword(input);
        }
    }

To my very very basic understanding: This my be the suspect area of concern, as I have “menu” to execute ShowMainMenu. Where I have an else if RunMainMenu lets players choose which level to chose from, and another else if for CheckPassword.

I sure hope this narrows it down. i have spent over an hour butting my head over this, adding/deleting lines, and stewing over the “reply” button… :crazy_face:

Neither C# nor Unity allow you to select something. When the OnUserInput method get called, the code gets executed and that’s it.

If the input value is not “menu”, the else-if conditions get checked. If the second one gets executed, the value of currentScreen is Screen.Password. The value of that variable does not change automatically. If you want to “restart” something, you have to set currentScreen to something else so the intended else-if block gets executed if the input value is not “menu”.

1 Like

Nina,

I added: currentScreen = Screen.MainMenu; to void start()
&

void OnUserInput(string input)

it appears to have fixed the issue. Thanks for your guidance :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms