Error Message Showing Up in Console I couldn't realize how to fix

Hello! I’m having an issue here with my Number WizardUI Game. It seems to work just fine on Unity but it isn’t working at all on Game Bucket. So I checked my Console and found this Error Message:

NullReferenceException: Object reference not set to an instance of an object
NumberWizard.NextGuess () (at Assets/NumberWizard.cs:44)
NumberWizard.StartGame () (at Assets/NumberWizard.cs:27)
NumberWizard.Start () (at Assets/NumberWizard.cs:19)

I realize that I’m having some issues with like 44, 27 and 19 here, but what exactly?
I’m really new to C#. I believe it’s a very simple error, but and even after Googling for some answers I couldn’t realize what to do. :slight_smile:

Here is the full code:

Hi there,

I had a similar error. The message “Object reference not set to an instance of an object” meant that I had forgotten, in Unity, to attach a Game Object to the corresponding reference in the code.

In your case it seems to come from your Text Object, called “text” (declared line 13).

In Unity, I assume that you have attached your script to a game object called “NumberWizard” as in the lecture. If you click on it in the hierarchy, you should see in the inspector (right hand side of the screen) a section corresponding to your c# script. In this section, you will see all the objects declared public. In your case you should see “Text”, “Chances” and “Max Guess Allowed”. I think that in your case, next to “Text” you should have an entry which indicates None. If this is the case, then all you need to do is take the Game Object that is supposed to be updated by the code (in the lectures it is called Guess), and drag it in this entry.

I hope this helps! :slight_smile:

Cheers!

3 Likes

Always remember, in Object Oriented Coding there must always be code attached to an object. (which is what the error Object reference not set to an instance of an object, indicates) don’t panic when you see it throwing all the line errors, read the error message carefully. I’m only speaking on this because when I first started coding in C# scripting in Unity it happened to me…Alot.
Take a look at the inspector information for whatever you attached the game code to (I forget what they called it in the tutorial). The script Component should have an exposed and empty (according to the error) location for your Text.text output. Drag and drop the appropriate Game Object from your Hierarchy to this location.

Keep hackin away at it,

E~

Privacy & Terms