I found a Bug! Game state doesn't update

While refactoring I found that even though we return to the main menu, the current screen is still Password, So while the code has returned us to the main menu the game state is still Password. I added a Screen.Mainmenu to the OnUserInput function to return the game state to the Main Menu state. Quite chuffed that I found this and corrected it on my own.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Hacker : MonoBehaviour

{

   // member variable Game State

   int level;

   enum Screen {MainMenu, Password, Win};

   Screen currentScreen = Screen.MainMenu;

   // Start is called before the first frame update

   void Start()

   {   

       

       ShowMainMenu();

       

   }

   void ShowMainMenu()

   {   

       

       Terminal.ClearScreen();

       Terminal.WriteLine("Hello Hacker");

       Terminal.WriteLine("What would you like to hack into?");

       Terminal.WriteLine("Press 1 for the Local Library");

       Terminal.WriteLine("Press 2 for the Police Station");

       Terminal.WriteLine("Press 3 for the NSA");

       Terminal.WriteLine("Press 4 for the GCHQ");

       Terminal.WriteLine("Enter your Selection.");

   }

   

   void OnUserInput(string input) 

   {

       if (input == "menu")   //we can always go directly to main menu 

       {

           ShowMainMenu();

           currentScreen = Screen.MainMenu;

       }

       else if (currentScreen == Screen.MainMenu)

       {

           RunMainMenu(input);

       }

   }

   void RunMainMenu(string input)

   {

       if (input == "1")

       {

           level = 1;

           StartGame();

       }

       else if (input == "2")

       {   

           level = 2;

           StartGame();

       }

       else if (input == "3")

       {   

           level = 3;

           StartGame();

       }

       else if (input == "4")

       {   

           level = 4;

           StartGame();

       }

       else if (input == "007")

       {

           Terminal.WriteLine("The name's Bond, James Bond.");

       }

       else 

       {

           Terminal.WriteLine("Please choose a level or type 'menu' to return to the main menu");

       }

   }

   void StartGame()

   {

       currentScreen = Screen.Password;

       Terminal.WriteLine("You chose Level " + level);

   }

   
   

}

Privacy & Terms