Hello! This error is happening again.
It seems to be triggered when I extract a method in Visual Studio. For some reason, this doesn’t work in the way that it does in the video - when I highlight the code I want to extract and right click > extract, it doesn’t go automatically to the name of the new method… for some reason it seems to be selecting Terminal instead, and then the ClearScreen becomes an error.
I poked around and it seems that when I extract the method it’s renaming this method in Terminal.cs to the name of the method I’m trying to extract?!
public static void ClearScreen()
{
primaryTerminal.displayBuffer.Clear();
}
I’m using a Mac. What’s going wrong here?
Here is the code from my Hacker.cs file:
using UnityEngine;
public class Hacker : MonoBehaviour {
// Game configuration data
string[] level1passwords = { "bookmark", "shelf", "loan", "hardback", "nap" };
string[] level2passwords = { "officer", "siren", "handcuffs", "complaint", "donuts", "arrest" };
string[] level3passwords = { "rocket", "planet", "comet", "asteroid"};
// Game states
int level;
enum Screen { MainMenu, Password, Win };
Screen currentScreen;
string password;
void Start ()
{
ShowMainMenu();
}
void ShowMainMenu()
{
currentScreen = Screen.MainMenu;
Terminal.ClearScreen();
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 NASA");
Terminal.WriteLine("Type menu at any time to return to the start");
}
// the user presses a key here, so the computer runs OnUserInput
// OnUserInput checks which screen we're on
// If we're on the Main Menu screen, it checks for a level selection - using RunMainMenu
// If we're already on a level, it checks for a password - using CheckPassword
void OnUserInput(string input)
{
if (input == "menu")
{
ShowMainMenu();
}
else if (currentScreen == Screen.MainMenu)
{
RunMainMenu(input);
}
else if (currentScreen == Screen.Password)
{
CheckPassword(input);
}
}
void RunMainMenu(string input)
{
bool isValidLevelNumber = (input == "1" || input == "2" || input == "3"); // defines valid levels
if (isValidLevelNumber)
{
level = int.Parse(input); // turns the input into an integer
AskForPassword();
}
else
{
Terminal.WriteLine("Not an option, sorry");
}
}
void AskForPassword()
{
currentScreen = Screen.Password;
Terminal.ClearScreen();
SetRandomPassword();
Terminal.WriteLine("Enter the password, hint: " + password.Anagram());
}
void SetRandomPassword()
{
switch (level)
{
case 1:
password = level1passwords[Random.Range(0, level1passwords.Length)];
break;
case 2:
password = level2passwords[Random.Range(0, level2passwords.Length)];
break;
case 3:
password = level3passwords[Random.Range(0, level3passwords.Length)];
break;
default:
Debug.LogError("invalid level number");
break;
}
}
void CheckPassword(string input)
{
if (input == password)
{
DisplayWinScreen();
}
else
{
AskForPassword();
}
}
void DisplayWinScreen()
{
currentScreen = Screen.Win;
Terminal.ClearScreen();
ShowLevelReward();
}
void ShowLevelReward()
{
switch (level)
{
case 1:
Terminal.WriteLine("Correct! You get a free book!");
break;
case 2:
Terminal.WriteLine("Correct! Have a GET OUT OF JAIL FREE card!");
break;
case 3:
Terminal.WriteLine("Correct! You win a trip to Mars!");
break;
}
}
void Update () {
}
}