[Solved] Getting null reference error when setting text from script

Hi everyone

So I am trying to run a Number Wizard game in which the player has the options of setting the extremities (min/max) from a list of buttons in the Start scene with UI buttons, and after that, when a random guess is made for the number, that guess cannot be printed on the text object on the Game scene. I get the nullrefence exeption error.

This is the Start Scene. The error appears after I click the Start button that takes me to the Game scene and the Guess cannot be printed to the UI Guess.

Observed behaviour
The compiler throws error: " NullReferenceException: Object reference not set to an instance of an object
NumberWizard.checkRangeStart (System.String name) (at Assets/NumberWizard.cs:123) "

Expected behaviour
I expect the UI Text to print the guessed number from the NumberWizard.cs since a gameobject NumberWizard was added into the Game scene and that object was connected with the UI Text on the Game scene.

Important information
I am currently using Unity 4.7.2f1

The script (error at line 123)

Things tried so far
I added a NumberWizard script component into the IU Guess variable and into that I connected the Guess variable, I’ve tried putting the value of the random guess to the UI Text into the Start() method, I made it so that the script can go from the Start scene to Game Scene first and then change the text of the UI Guess (which is located in the game scene)

I hope I’m clear in expressing my problem. Been stuck for a few days with it. Please Help

I think you should remove the NumberWizard.cs component from the Guess text box, it should only be on the NumberWizard game object, and make sure all the Text public variables are properly set in the inspector. Apologies if you’ve already checked this, I just can’t see it from those screenshots.

Adding that component to the UI Guess was one of the things I tried to fix the problem. But it didn’t work. I removed it now and I still get the same error. The Text variables in NumberWizard are all public.
I am using the same NumberWizard.cs on both scenes of Start and Play (on different game objects), and three Text variables are used in the Start scene while the Guess variable is used in the Game scene. Could this be the reason for the error ?

Oh right, I see. You should probably refactor your code so that you use the NumberWizard script only in the Game scene, and move the rest of your code to something like a RangeSetter script on a new gameobject, that would avoid these kinds of errors. Here I think what’s happening is that your checkRangeStart method gets called in the Start scene, when textGuess isn’t yet assigned.

Ah, yes, that seemed to fix the problem. Thank you :slight_smile:

I created a new class now called WizardGame but the Min and Max variables are in the NumberWizard class.
I made the max and min variables public then I tried inheriting like this:

public class WizardGame : NumberWizard { …}

but I get this error:
Assets/WizardGame.cs(10,33): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `NumberWizard.min’

Why does this happen?!

I feel this kind of inheritance is a bit advanced for this part of the course :slight_smile: It is not necessary for you to use this here. If your problem is getting some values from one scene to another, I suggest looking into the DontDestroyOnLoad() method. With this you can have a gameobject that persists between scenes, along with the values you want to pass.

Probably yes :slight_smile: But if I use the DontDestroyOnLoad() method, does that save the values on the NumberWizard script in the form of textMin and textMax even after the scene is changed? So that I could access their values.

Also when you press Play on the editor do all the scripts get compiled at the same time?

Edit: I fixed the problem. Did some googling around about how to save values between scenes and apparently all you had to do was to make the min and max variables in NumberWizard.cs to be Static. In that way they could also be accessed from other scripts on other scenes (levels).

Thanks for the help @Sebastian_Martens :slight_smile:

1 Like