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:
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:
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:
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.
Create my public Text text; , which makes it pop up into the Inspector of the text that the script is attached to.
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?
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
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.
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
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.