Hi guys,
Whenever I add the ‘Passwords.Anagram()’ method to my code, I keep getting an ‘Object not set to an instance of an object’ error.
I was wondering if anyone else had the same issues? I read that someone else had the issue when they were missing their Utility.cs file, but I have mine there in the Assets folder.
using UnityEngine;
public class Hacker : MonoBehaviour {
//Game configuration data
string[] level1Passwords = { "password1", "password2", "password3", "password4", "password5", "password6" };
string[] level2Passwords = { "password 7", "password8", "password9", "password10", "password11", "password12" };
//Game state (where we put all of our member/global variables). an enum is a variable type that we create ourselves (like int/float etc but one we make up), and give it a list of possible states
int level;
enum Screen { MainMenu, Password, Win };
Screen currentScreen;
string password;
// Use this for initialization
void Start () {
ShowMainMenu("Good morning Sophie,");
}
void OnUserInput(string input)
{
if (input == "menu")
{
ShowMainMenu("Good morning Sophie");
}
else if (currentScreen == Screen.MainMenu)
{
RunMainMenu(input);
}
else if (currentScreen == Screen.Password)
{
CheckPassword(input);
}
}
//have declared functions down here
void ShowMainMenu(string greeting)
{
currentScreen = Screen.MainMenu;
Terminal.ClearScreen();
Terminal.WriteLine(greeting);
Terminal.WriteLine("Please select from the below:");
Terminal.WriteLine("Press 1 for Library");
Terminal.WriteLine("Press 2 for Law");
Terminal.WriteLine("Press 3 for IT");
Terminal.WriteLine("Enter your selection:");
}
void RunMainMenu(string input)
{
//check if the level number is valid, and if it is, convert it to integer
bool isValidLevelNumber = (input == "1" || input == "2");
if (isValidLevelNumber)
{
level = int.Parse(input);
StartGame();
}
else
{
Terminal.WriteLine("Invalid choice. Please try again");
}
}
void StartGame()
{
currentScreen = Screen.Password;
Terminal.ClearScreen();
Terminal.WriteLine("You have chosen level " + level);
RunGame();
//use switch if we're comparing two values, rather than if (although if/else does the same). in the case that level = 1, do this..etc. default is the else clause
switch (level)
{
case 1:
//select random number between 0 and the number of items in the level1Passwords array. don't have to store in a separate variable
password = level1Passwords[Random.Range(0, level1Passwords.Length)];
break;
case 2:
password = level2Passwords[Random.Range(0, level2Passwords.Length)];
break;
default:
Debug.LogError("Invalid level choice");
break;
}
}
void RunGame()
{
currentScreen = Screen.Password;
Terminal.WriteLine("Enter password: hint " + password.Anagram());
}
void CheckPassword(string input)
{
if (input == password)
{
DisplayWinScreen();
}
else
{
Terminal.WriteLine("Password incorrect. Please try again: ");
}
}
void DisplayWinScreen()
{
currentScreen = Screen.Win;
Terminal.WriteLine("You guessed correctly!");
ShowLevelReward();
}
void ShowLevelReward()
{
switch (level)
{
case 1:
Terminal.WriteLine("Here's a book. Yay!");
Terminal.WriteLine(@"
_____________
/ / /
/ story / /
/ / /
/__________/ /
(__________( /
");
break;
case 2:
Terminal.WriteLine("Uh oh. You're going to jail!");
Terminal.WriteLine(@"
______ ______
// \\ // \\
|| ||88888|| ||
\\______ // \\______//
");
break;
default:
Terminal.WriteLine("Invalid choice");
break;
}
}
void Update()
{
}
}
When I run the above, I get the attached screenshot. I lose my ‘enter your password’ text and get the object not set to an instance of an object error.
When I remove the passwords.Anagram() method, I get my ‘enter your password’ text back!
Now I’m hoping I’m being really stupid and missing something obvious; can anyone advise at all?
Thanks for your time,
Sophie
PS, I should mention that I’ve tried reimporting the .Unity file to no avail.