Hi Tommy,
When I run your game, after making a selection from the main menu an error message is thrown.
NullReferenceException: Object reference not set to an instance of an object
StringExtension.Shuffle (System.String str) (at Assets/Scenes/WM2000/Utility.cs:16)
StringExtension.Anagram (System.String str) (at Assets/Scenes/WM2000/Utility.cs:5)
Hacker.askforpassword () (at Assets/Scenes/Hacker.cs:118)
Hacker.RunGameMenu (System.String input) (at Assets/Scenes/Hacker.cs:77)
Hacker.basicinput (System.String input) (at Assets/Scenes/Hacker.cs:59)
Hacker.OnUserInput (System.String input) (at Assets/Scenes/Hacker.cs:47)
These can often seem a little daunting but if we follow this from the bottom upwards we can see the first file that is mentioned is your Hacker.cs script, and that the issue relates to line 47. Let’s start there…
Line 47 is this;
basicinput(input);
so we follow that method call which takes us to line 51, determines which screen you are on, this condition is true;
else if (currentScreen == Screen.MainMenu)
So this statement executes, note this is line 59 which was the next line up in the error detail above;
RunGameMenu(input);
Now let’s look at that method, this one is handling what you have selected as an option, I chose “1” on my attempt which threw the error, so that takes us to line 77, the third line up in the error message above, and this statement;
askforpassword();
If we now look at that method we can see that this line is 118, which is the fourth line up from the bottom in the error detail above;
Terminal.WriteLine("password hint:" + password.Anagram());
As you are getting a NullReferenceException
error an object in this statement must be null
. The error is thrown when you try to access a member of that object, in this specific case you are trying to access the Anagram
method of the password
object, and password
is null
.
We can both test and prove this by adding this line to the beginning of the asforpassword
method;
Debug.Log(password);
When you run the game and make the same choice (option 1) now, you’ll see the word “null” output to the console. So the issue here is that a password has not been selected.
If we look at what else your code in the askforpassword
method does we can see that you do in fact attempt to set the password, but after you have tried to use it, so, to resolve this issue, move these line;
Terminal.WriteLine("password hint:" + password.Anagram());
to be after the switch
statement, e.g.
void askforpassword()
{
currentScreen = Screen.Password;
Terminal.WriteLine("you are entering level " + level);
switch (level)
{
case 1:
password = level1passwords[Random.Range(1, level1passwords.Length)];
break;
case 2:
password = level2passwords[Random.Range(1, level2passwords.Length)];
break;
case 3:
password = level3passwords[Random.Range(1, level3passwords.Length)];
break;
default:
Debug.LogError("invalid level no.");
break;
}
Terminal.WriteLine("password hint:" + password.Anagram());
}
With the above, you now determine what the password should be, based upon the level selection, you initialise the password object, and then afterwards, you access it’s method, Anagram
.
Run the game now and you’ll find the NullReferenceException
error is not thrown and your password is displayed.
A couple of other minor points, it would be best to follow a standard naming convention in your code, typically class names, properties and methods would use PascalCase and variables would use camelCase. This can make it a lot easier to tell, by glancing at the code, what’s what. As an example, your method askforpassword
would be AskForPassword
, basicinput
would be BasicInput
, at the moment you have quite a mix of different conventions going on and this will only make it more challenging for yourself and anyone else who looks at your code. It would be worth spending a few minutes to clear up these now before it becomes a habit.
In the AskForPassword
method, I’d probably move both lines which display a message to the user to be together also, whilst I suggest above about only moving the one, that was merely to remove the error, it would make more sense that the statements which display output to the user are together, that would look like this;
void AskForPassword()
{
currentScreen = Screen.Password;
switch (level)
{
case 1:
password = level1passwords[Random.Range(1, level1passwords.Length)];
break;
case 2:
password = level2passwords[Random.Range(1, level2passwords.Length)];
break;
case 3:
password = level3passwords[Random.Range(1, level3passwords.Length)];
break;
default:
Debug.LogError("invalid level no.");
break;
}
Terminal.WriteLine("you are entering level " + level);
Terminal.WriteLine("password hint:" + password.Anagram());
}
Hope this helps
See also;