[SOLVED] NumbersWiz UI (Section 4) lost in NullReferenceException

NullReferenceExceptions on first call of Public Text text

bitbucket: numberswizui552f1

I have checked the obvious spots I can think of but cannot seem to figure out where I’m messing up the references. Would like to ensure that I’m getting things right and not making silly mistakes as I move through the course.

Both Start & Game scenes have a NumberWizard script objects; in both scenes there are public text variables assigned to UI text objects.


####Expected Behaviour:
The UI text objects’ contents should be controlled by the NumberWizard script. No more, no less.

####Actual Behaviour:
The code throws error to console. Assets/Scripts/NumberWizard.css/NextGuess() line 77 on first call of a Public Text object.
The script does edit the UI text fields as desired, albeit with errors thrown to console.


Error persists in 4.7, 5.5 and 5.6 beta. (I’m only in 5.x so as to export WebGL)

Interestingly, in 5.6 beta I created two empty objects with the same names as the ‘missing’ objects from the script and pointed the NumberWizard Text object targets to them. No ensuing codes.

In the Start scene, the Text targets are tied into two UI text empties and no error is thrown.
In the Game scene, the Text targets are tied to two UI text fields and errors are thrown.


Any insights would be appreciated.
Ta.

So the issue seems to be with assigning public text objects from code in conjunction with Unity Editor linking.

Quin DP helped in the Discord channel.

Solution:

make both text fields private and then hook them into gameobjects in Start():

	guessText = GameObject.Find("subtitle").GetComponent<Text>();
	countText = GameObject.Find("subtitle").GetComponent<Text>();
2 Likes

further:

JohnnyDalvi from the Discord channel pointed out that for some asinine reason I had an instance of numberwizard.cs called from the canvas gameobject; this meant I was calling references to text objects from the same script twice - once linked to objects, and the other not.

As this instance had no links to the text object variables it was meant to set and thus the nullreferenceerrors…

1 Like

I’m glad your problem was solved @nomadicarus !
Debugging is a very good experience from the learning perspective

Updated Mon Mar 06 2017 11:01
I’m JohnnyDalvi from the Discord channel, I’m usually more active here (although I haven’t been as online as I wanted to lately)

1 Like