Unable to set UI Text using C# Script (NullReferenceException)

Greetings. Hope you are all having a great day.

I am trying to create NumberWizard UI by only using the GDD. However, I have run into some issues when trying to use Text.text (Like we did in Text101) to change the value of a UI Text field. It is giving me a NullReferenceException and I am confused as to why it would happen. Here is a simple code which replicates the error together with the screenshot to showcase that the button is set up correctly:

Link to Pastebin for Code

  • Expected Behaviour:

Upon start of the program, the text field should display “Derp” without any errors. When clicking the “Higher” button, it should run the HigherButton(); function and set that text value to “Test”.

  • Observed Behaviour:

The Derp comes up in the start but still prints out the NullReferenceException error. Upon clicking the Higher button, the text does not change and just prints out an error for each time the button is clicked.

I have already tried the following:

  1. Used only the start and update functions to attempt at changing the text directly. Gives out the same error.

And… Well that’s pretty much it. I could not think of anything else to try at this point, apologies. Any tips towards bugshooting would be appreciated! Thank you for taking your time to read thus far, and I hope you have some good hints!

Best Regards,
Amar.

**

EDIT WITH SOLUTION:

**

It seems as if the main root of this problem lied with the GameObject being used with the button.
I had simply forgotten to add the “guess” from the hierarchy into the GAMEOBJECT scrip section as shown below:

This makes sense with the NullReferenceException, as it was trying to change the value of something that… Simply did not exist. (Aka NULL).

Be careful with assigning your components and take care everyone.

Special thanks to @Rob for helping me figure this out! Kudos!

Best Regards,
Amar.

Are you dragging the instance of the GameFunctions that is inside the hierarchy? If you drag it from the project folder it won’t work as expected

Edit:
I mean… Regarding the GameFunctions script within the button

In your lower screenshot you only appear to have ‘Guess’ exposed and referencing the ‘Guess’ text object in your hierarchy.

How are you trying to access the Text text object? You haven’t exposed a public variable to reference it in through the Inspector, so unless you are finding it in code you won’t have any reference to it. As such it will be null. The error is then thrown because you are trying to access the text property of an object with a null reference.

If you aren’t finding the object in code then expose it as a public variable as you have with ‘Guess’ and drag it into the Inspector to create the reference.

I am trying to access the Text text object in the same way I was attempting to do it in Text101.

  1. Create my public Text text; , which makes it pop up into the Inspector of the text that the script is attached to.

  2. Simply drag the text element from the hierarchy to the field in the inspector.

The way I did it in NumberWizard now is exactly the same thing (In my mind). I fail to see why it should return null. In my inexperienced mind it seems as if i’m doing the same thing but not getting the same result. I’m sorry, Rob. But I just can’t seem to understand what you’re trying to get at.

I seem to have found a way to make it not give out NullReferences, but in a way that it only causes more confusion and questions.

If I REMOVE the GameObject which I also have attached the script to, and simply ONLY have the script attached to the text element, it does not give any errors and does change the text.

However that makes me struggle a bit. I would like to use this script to control the buttons “Higher” and “Lower”, changing the text element with functions. So I guess my question now is:

How does having a script attached to both a GameObject AND a UI Text element create a null reference?

This is how I thought you would be approaching it, but if you look at your second screen shot;

…in the Inspector you only have “Guess”, you need “Guess” and “Text” there with both of the text objects dragged across respectively…

Just to check I understand your problem correctly @Amar_Duranovic, you are trying to use two text objects and populate them both from code, yes?

1 Like

I am trying to change the value of ONE text object. However I wish to do this by clicking on a button. The only text I wish to manipulate from the code is the “Guess” text. I guess I need a more general understanding of how the text objects in the Hierarchy coincide with the inspector and code.

If I define a public Text text; in my code, that means I create a box in the inspector named “Text”. That “Text” can then be assigned a text object from the hierarchy in order to specify which text in Unity I am manipulating through the code. And in order to access that text property, I simply use text.text = “Whatever”;

That is how I understand it. If there is something wrong with my fundamental understanding of the process, please correct me, as I believe a lot of my confusion is coming from this :smile:

Thank you in advance for all your time, @Rob !

Edit:

Let me show you a bit what is causing me the confusion directly.

In the GIF above you see my code and execution. This works fine.

However, if I try to attach the script to a gameobject so I can use it with a… IT WORKS

AND I KNOW WHY.

I had forgotten to assign GUESS to the GameObject part of the inspector. As so:

Thank you SO much for your time, @Rob ! You’re the best, made me realize my own mistake through your feedback. Thank you! This was a great learning experience.

I will edit the initial post with how to solve the issue for any future viewers.

Best Regards,
Amar.

1 Like

Hi @Amar_Duranovic,

Ok, great, I understand now - thanks :slight_smile:

Yes, you are correct that is how you can do it.

I would perhaps try to remove some of the confusion through the names that are being used here also.

So for example, you currently have two text objects in your hierarchy, one called Text and one called Guess, having had a closer look at your hierarchy I am guessing that Text is the instruction at the top, if so, perhaps rename it to “Instructions”. That way, if you refer to it in code at any point you would state Instructions.text = rather than Text.text which can get confusing.

So, lets work out what the issue is.

“Derp” appears when you run the game because you have specified this in the Text field of the Text script component on the Guess game object.

  • You have a GameFunctions object in your hierarchy, does this have the GameFunctions script attached to it?
  • Your Higher button has an OnClick() event and you have added GameFunctions to that, but is that the “script” from the Project view, or is that the game object named GameFunctions from your hierarchy (it needs to be the latter as per @Joao_Dalvi’s response above).

Would also be useful if you could post your script(s), specifically GameFunctions.cs


See also;

1 Like

I figured it out, as shown by the edit above. But thank you for the tip about the instructions text. I am generally very structured about my names, I just wanted to get a test case up and running to show the issue.

Thank you yet again for your time, Rob!

1 Like

hehe, our posts appear to over-lapped.

Ok - so sounds like you’ve got it all solved… well done :slight_smile:

1 Like

Privacy & Terms