[PROBLEM] UI Objects not updating

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

  1. Pressing the ‘begin’ button loads ‘SetUp’ and calls Start();
  2. 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 ();
  3. User uses GuessHigher(); and GuessLower(); and either the win or lose level is loaded based ont he result.
  4. 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.

Been chatting with a few people on Discord and they’re suggesting it’s likely because the objects I’m creating are being destroyed between scenes, including my text objects… still haven’t found a solution however!

Hello @Liam_Oliver Liam, how are you?

To fix it you will want to use the DontDestroyOnLoad() method within a script that holds the player preferences, you will have to save the numbers from the setup in this new script (just make a method to pass the values of the preferences before changing scene when you press Start):

—inside the new script named “PlayerSavedPreferences”----


public int prefGuesses;
public int prefMax;
public int prefMin;

void Start()
{
    DontDestroyOnLoad();
}

public void savePreferences(int min, int max, int guesses)
{
     prefGuesses = guesses;
     prefMin = min;
     prefMax = max;
}

— and call it within the setup script before loading the next scene—


public void startNextScene() //call this with the button instead of calling the levelmanager directly
{
     FindObjectOfType<PlayerSavedPreferences>().savePreferences(setupMin,setupMax,setupGuesses);
     FindObjectOfType<LevelManager>().LoadScene("Game"); //don't remember this method's name, you will have to fix this line
}

And then you will want to make your NumberWizard.cs take the values of this new script and pass it to it own min,max and AllowedGuesses (I’ve sent an fork to the github gist with the modification on the NumberWizard.cs done too).

I hope that this helps, let me know if you manage to make it work.

Best Regards,

Hi Joao (@Joao_Dalvi) ,

Thank you very much for your reply!

I’m a little confused as to where I need to place your ‘startNextScene()’ method/function.

I tried to add it to my NumberWizard.cs script and i got the following errors:
CS0103: The name ‘setupMin’ does not exist in the current context (CS0103) (Assembly-CSharp)
CS0103: The name ‘setupMax’ does not exist in the current context (CS0103) (Assembly-CSharp)
CS0103: The name ‘setupGuess’ does not exist in the current context (CS0103) (Assembly-CSharp)
CS1061: ‘LevelManager’ does not contain a definition for ‘LoadScene’ and no extension method ‘LoadScene’ accepting a first argument of type ‘LevelManager’ could be found (are you missing a using directive or an assembly reference?)

Do I need to write a new script to store this method and write new variables?

Ideally, I think I’d like to have something like this in my NumberWizard.cs:

public void Start ()
{
savedPrefs = FindObjectOfType(); // You need to cash the reference to this script
StartGame ();
}

//initializes all integer variables and updates all text for the UI afterwards
public void StartGame ()
{
FindObjectOfType().savePreferences(setupMin,setupMax,setupGuesses);
FindObjectOfType().LoadScene(“Game”);> max = savedPrefs.prefMax; // Passing the Max Value from the player preferences to the max guess
min = savedPrefs.prefMin; // Passing the Min Value from the player preferences to the min guess
maxGuessesAllowed = savedPrefs.prefGuesses; // Passing the Guesses Value from the player preferences to the number of guesses
NextGuess (); // most recent guess recorded by the computer
UpdateText ();
}

So that the in ‘SetUp’ screen/level, the player alters the values stored in the ‘PlayerSavedPreferences’ script, then when the user clicks ‘Start’ the ‘Game’ scene is loaded, the preference variables are then passed to the appropriate variables in the NumberWizard.cs script, a guess is made and the text on screen is updated appropriately.
I’m thinking I should probably put my preference changing methods (in the NumberWizard.cs script in the #region) within the ‘PlayerSavedPreferences’ script too so that it’s easier to manage the variables from within that script. Thoughts?

Thanks again for your help!

  • Liam.

Liam, the StartNextScene() method should go inside the script that you are using to let the player change the min, Max and guesses value and use these variables as the setupMin, setupMax and setupGuesses

This is what has to happen:

Player changes the values > values get saved in a GameObject that won’t be destroyed on load(PlayerSavedPreferences in this case) > NumberWizard.cs retrieve the values saves to this script.

You won’t be able to use the NumberWizard.cs to call savePreferences since it won’t be available on the scene that the player changes the preferences :frowning:

I was assuming that you were using another script to alter the min/max value on the start scene, but you can do it in the PlayerSavedPreferences, it is even better since you won’t even need to use the savePreferences method (you would be changing within the PlayerSavedPreferences script itself).

Regarding your modifications to the NumberWizard.cs, the two lines that you added regarding the StartNextScene won’t work in this situation, those two lines are suposed to call the change of scene from the start scene to the game scene (I was supposing that you had an script to handle the modifications of the min/max/guesses variables on the setup screen, you could use this method from this supposed script). But as you are willing to use PlayerSavedPreferences as the script to handle the changes on the setup, you won’t even need to use the StartNextScene method since the changes are being made within PlayerSavedPreferences and you can still change the scene using the regular LevelManager method.

Privacy & Terms