[Solved] Game.Unity Problem

I am having a problem with the guesses and number of iterations the game has gone though on the main Game screen.

Now the Higher button works fine but I am having setting the initial values in the StartGame() Function I didn’t like the idea of putting actual text of “500” in the text box to start. So I tried putting the command in the StartGame():

guessText.text = guess.ToString();

And I got the following error:

   NullReferenceException: Object reference not set to an instance of an object

And I can’t understand why that is? And I also can’t understand why the numerical vars can’t be set in the StartGame Function like for instance the max was originally set at 1000 in the first Number Wizard, and I remembered that we needed to add 1 to the variable but: max=max+1; would not work. If I don’t explicitly state that at the top of the class before the functions I can’t set max correctly at all.

To anyone who can help here is a big thankyou in advance!


Good Morning,

if that error is related to the line you are showing then it would appear that guessText has not been assigned.

the reason that Im saying that is , guessText is a reference type variable, ie, it refers to or points to another object/class. if a reference type variable is not assigned anything to point to then it has a NULL value, so at any time you go to try and use it, the compiler will give you that error (exception) saying that it cant do anything with it since its null.

things to check, if guessText is public, make sure that it has been assigned something in the inspector.

Not sure what you mean by setting max?
im guessing that it might be to do with the scope of the variable, but if you could post your script, see if we can help a bit further.


Thankyou for taking the time to respond.

With your reply I was sure that I had everything linked/targeted up correctly so I took a careful look at the project itself and found that in the canvas area itself there were two more numberwizard.cs scripts added in the components area and of course, they weren’t linked up at all. No wonder there were problems :laughing:.

I had three numberwizard script all attached to the project!

Thanks again for your response it made the error easier to find :sunglasses:.


1 Like

Privacy & Terms