How to this if else work

There is no error in my code but just want to know why after selecting option 1 or 2, it starts the game and no other option seems to be working except typing “menu” when it takes back you the Main Menu. I am unable to understand why selecting option 1 or 2 and then typing “menu” works.
Hope you all got my question

Hi Faesal,

Unfortunately, without knowing anything about your project, it’s impossible for us to tell you what is happening in your code.

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

public class Hacker : MonoBehaviour {
int Level;
enum Screen { MainMenu,Password,Win}
Screen CurrentScreen = Screen.MainMenu;
// Use this for initialization
void Start ()
{
ShowMainMenu();

}

void ShowMainMenu()
{
    
    Terminal.ClearScreen();       
    Terminal.WriteLine("This Game is About Arraging Words");
    Terminal.WriteLine("Press 1 for Library");
    Terminal.WriteLine("Press 2 for Police Station");
    Terminal.WriteLine("Enter Your Selection:");
}

void OnUserInput(string input)
{
     if (input == "Menu")
    {
        ShowMainMenu();
        CurrentScreen = Screen.MainMenu;
        Level = 0;
    }
     else if (CurrentScreen == Screen.MainMenu)
    {
        RunMainMenu(input);
    }

}

void RunMainMenu(string input)
{
    if (input == "1")
    {
        print("You Chose 1");
        Level = 1;
        StartGame();
    }
    else if (input == "2")
    {
        print("You chose 2");
        Level = 2;
        StartGame();
    }

    else
    {
        Terminal.WriteLine("inlvalid option");
        print("inlvalid option");
    }
}

void StartGame()
{
    Terminal.WriteLine("You Have Chosen Level " + Level);
    Terminal.WriteLine("Please Chose a Password:");
    CurrentScreen = Screen.Password;
    
}

}

Your code looks for “Menu”, not “menu”. If you are in doubt whether the user input is processed correctly by your methods, log the input into your console with Debug.Log("Input: " + input);.


See also:

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

Privacy & Terms