Password Doesn't Accept, Although Password is Displayed Correctly

Hello Everyone,

I am having issues using the Random Behavior. I followed the similar example in the lecture (used different variables) and when I type in the password that displays on the bottom right corner of Unity, it continues to display my error code “Incorrect Password, Try Again!”. For example, if I type in “Mushroom Kingdom” as the level and it asks to type in the password (Let’s say the password is Luigi) and it is shown in Unity under the debug menu and I type in “Luigi” as the password, it will display my error code: “Incorrect Password, Try Again!” until I continue to type in any password in the array until it accepts it. Below is my code so far:

public class Hacker : MonoBehaviour
{
    //Game Configuration Data
    string[] levelMushroomKingdomPasswords = { "Mushroom", "Mario", "Luigi", "Toad", "Bowser", "Power Star" };
    string[] levelRPDStationPasswords = { "Virus", "Leon", "Chris", "Nemesis", "Wesker", "Claire" };
    string[] levelPersona5Passwords = { "Joker", "Arsene", "Ryuji", "Last Surprise", "Morgana", "Yusuke" };

    //Game State
    string level;
    string password;

    enum Screen { MainMenu, Password, CheckPassword, WinScreen, }; //
    Screen currentScreen;

    // Start is called before the first frame update
    void Start() //Starts the main menu
    {
        ShowMainMenu();
    }
    void ShowMainMenu() //Displays Main Menu
    {
        currentScreen = Screen.MainMenu;
        Terminal.ClearScreen();
        Terminal.WriteLine("Welcome to the terminal screen. Please select a system to hack: ");
        Terminal.WriteLine("Mushroom Kingdom");
        Terminal.WriteLine("RPD Station");
        Terminal.WriteLine("Persona 5");
        Terminal.WriteLine("Enter your selection:");
    }

    void OnUserInput(string input) //The user enters a response
    {
        if (input == "Menu") //We can always go directly to the main menu
        {
            ShowMainMenu();
            currentScreen = Screen.MainMenu;
        }
        else if (currentScreen == Screen.MainMenu) //This is if we are on the main menu
        {
            RunMainMenu(input);
        }
        else if (currentScreen == Screen.Password) //This is if we are on the password screen
        {
            PasswordInput(input);
            currentScreen = Screen.Password;
        }
    }

    void RunMainMenu(string input) //This is running the main menu. User inputs characters to select a system to hack
    {
        bool isValidLevel = (input == "Mushroom Kingdom" || input == "RPD Station" || input == "Persona 5");
        if (isValidLevel)
        {
            level = input;
            EnterPasswordScreen();
        }
        else if (input == "007")
        {
            Terminal.WriteLine("Not even you 007 can hack this, try again!");
        }
        else
        {
            Terminal.WriteLine("Error! Please select a valid system to hack");
        }

        if (input == "Mushroom Kingdom")
        {
            password = levelMushroomKingdomPasswords[Random.Range(0, levelMushroomKingdomPasswords.Length)];
        }
        else if (input == "RPD Station")
        {
            password = levelRPDStationPasswords[5];
        }
        else
        {
            password = levelPersona5Passwords[3];
        }
    }

    void EnterPasswordScreen() //This displays the screen to enter a password
    {
        currentScreen = Screen.Password;
        Terminal.ClearScreen();
        Terminal.WriteLine("You have chosen " + level);
        Terminal.WriteLine("Enter a password: ");
    }

    void PasswordInput(string input) //This is when the user enters the correct password
    {
        switch (level)
        {
            case "Mushroom Kingdom":
                int index1 = Random.Range(0, levelMushroomKingdomPasswords.Length);
                password = levelMushroomKingdomPasswords[index1];
                break;
            case "RPD Station":
                password = levelRPDStationPasswords[5];
                break;
            case "Persona 5":
                password = levelPersona5Passwords[3];
                break;
            default:
                Debug.LogError("Try Again!");
                break;
        }

        Terminal.WriteLine("Hint: " + password.Anagram());

        if (input == password)
        {
            WinGame();
        }
        else
        {
            Terminal.WriteLine("Incorrect Password, Try Again!");
        }

    }

    void WinGame() //Displays the Win Screen
    {
        currentScreen = Screen.WinScreen;
        Terminal.ClearScreen();
        ShowLevelReward();
    }

    void ShowLevelReward()
    {
        switch(level)
        {
            case "Mushroom Kingdom":
                Terminal.WriteLine("You now have access to Mushroom Kingdom!");
                Terminal.WriteLine(@"
     ______________
    /             //
   /             //
  /             //
 /_____________//
(______(______(/
");
                break;
            case "RPD Station":
                Terminal.WriteLine("You now have access to RPD Station!");
                break;
            case "Persona 5":
                Terminal.WriteLine("You now have access to Persona 5!");
                break;
        }
    }
    
    // Update is called once per frame
    void Update()
    {
        int index = Random.Range(0, levelMushroomKingdomPasswords.Length);
        print(index);
    }
}

type or paste code here

Hi Neil,

Log the input value into your Unity console (not the Terminal) when OnUserInput gets called. Also do that in the PasswordInput method. When you pass on a meaningful message along with the values, you should be able to see if what you typed and what the computer passes on to your methods respectively.

If the input is what you expected, log the password into your console. Maybe its value is “wrong”.

This topic was automatically closed after 3 days. New replies are no longer allowed.

Privacy & Terms