This error is simply because you have not provided a reference to the Text object via the Unity Editor’s inspector. Having not set it in the inspector, when the code runs, it is null.
Also, please use the code tags (see help area of forums) when posting code for readability.
overwrites whatever is set in the inspector. If you have dragged a specific text component into the inspector field you don’t need this line.
edit: if you aren’t setting it in the inspector, this script needs to be on the text child object, not the canvas/gameobject etc. and you keep the line, although you can just have
Good eyes @LeftyRighty … without the code formatting I completely missed that .GetComponent<>() call.
To add to @LeftyRighty 's post above, if you ( @Llufes ) are doing the .GetComponent<>(), then this script must be on the same GameObject that the Text component is on.
Just as an advice, you should not use text as a var name, as text is already a part of build in components. Anyway, your code is working for me. I just created an Canvas->Panel->Text object, put the text component on it if it wasn’t already there and put your script on it. Just changed the score thing to 1f to test, and it counts up every second.
Make sure also that there is not some OTHER game object in your scene that has the script on it also. Sometimes a careless mouse drag will put the script on two objects.
At the start of the Awake() function,print out the name field (this.name) and make sure the error you see is on the game object you expect it to be,
Some times you have the answer in front of you and you can’t manage to see it. After all, @Kurt-Dekker you were right… Another gameobject had the same script… Thank you all for your help!
You’re welcome. I remember the first time that exact bug bit me and it took hours to figure out. Now it is simply one more thing that I check whenever I start getting really weird results like “hey, there IS a Text component, I’m looking at it! What the heck is going on!?”