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:
- 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:
- 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.