Hey Guys,
Here’s my code: Link to code
I tried to expand my Number WIzard UI game by including a preferences screen. To do this, I created another ‘level’ called ‘SetUp’ and added some methods which increased or decreased the max and min values of the range aswell as the number of guesses the computer is allowed to make.
I then created some new text variables to display the range, the maximum value of the range, the minimum value of the range and the number of guesses allowed.
I also created a new method called ‘Update Text’ which ‘SHOULD’ update all these text variables (including the generic ‘text’ variable we created in the lecture to record the guess made by the computer) so that if they need to be displayed on in the level, they are updated when the method is called.
To transition between levels I use the LevelManager script outlined in the lectures.
the basic structure of my game is:
Start 1.> SetUp 2.> Game 3.> Win/Lose 4.> Start
- Pressing the ‘begin’ button loads ‘SetUp’ and calls Start();
- The user manipulates the max, min and maxGuessesAllowed variables (by using the methods in the #region preference setting area of the code - link above!) then presses the ‘Start’ button which calls NextGuess ();
- User uses GuessHigher(); and GuessLower(); and either the win or lose level is loaded based ont he result.
- User presses the 'Try again method and starts the game afresh.
Here’s some screenshots of the Start, SetUp and Game levels with the text objects linked to the NumberWizard script shown:
Screenshot 1: The Start Level
Screenshot 2: The SetUp Level
Screenshot 3: The Game Level
Now for the problem:
Expected Behaviour
After the user has chosen thier preferences on the ‘SetUp’ level they then press 'Start and this begins by loading the ‘Game’ level, then begins the ‘NewGuess’ method which should make a new guess, decrease the remaining guesses and update all the text variables (displaying to the user what the current range the computer is guessing from in the ‘Range’ text object, how many guesses are left in the ‘Guesses’ text object and what the guess was in the ‘Text’ object.
Observed behaviour
The ‘SetUp’ screen displays the text objects correctly and updates the text as the user clicks the ‘-’ and ‘+’ buttons.
When loading into the ‘Game’ level none of the text is displayed by the text objects but the computer is clearly making guesses as randomly clicking ‘higher’ and ‘lower’ will eventually lead you to the win screen.
I am also getting an error in the console which reads:
NullReferenceException: Object reference not set to an instance of an object
NumberWizard.UpdateText () (at Assets/NumberWizard.cs:38)
NumberWizard.StartGame () (at Assets/NumberWizard.cs:32)
NumberWizard.Start () (at Assets/NumberWizard.cs:22)
EDIT: I also know that the program is recording the number of guesses chosen by the user in the ‘SetUp’ levelas pressing the higher and lower button by the number of guesses chosen, works.
What have I tried to fix it?
I’ve been at this trying to fix it for the better part of a da; I’ve tried ALOT of things. I’ve googled the error, attempted to follow the unity manual and had no luck.
I also made all the methods and variables public to ensure everything had access to everything…
Let me know if you have any ideas on how to fix this error or if you’d like me to explain anything about my code or if you’re confused by my explanation.
Cheers!
- Liam.



