- Home /
Javascript UIText not set to an instance of an object
All,
I followed a tutorial for a timer. The values all are stored and updating correctly. However, the UIText used to display the time doesn't seem to be working. I get the error,
"Object reference not set to an instance of an object" even though I have dragged the object to the correct field in the inspector.
This is my code:
var startTime : float;
var textTime : String;
var guiTime: float;
var minutes : int;
var seconds : int;
var fraction : int;
var textField : UI.Text;
function Start() {
startTime = Time.time;
textField = GetComponent(UI.Text);
}
function Update () {
guiTime = Time.time - startTime;
seconds = guiTime % 60;
fraction = (guiTime * 100) % 100;
textTime = String.Format ("{0:00}:{1:00}:{2:00}", seconds, fraction);
***textField.text = textTime;***
}
The last line, textField.text = textTime;
is where the error is.
Can anyone give me help with this?? I'm boggled why, although I've linked the object to the variable in inspector, and it even shows that I have, I am getting an error saying it is not referenced.
Thank you
Answer by vvcc · Mar 12, 2018 at 09:59 PM
Figured it out - I was writing over it in Start(), with textField = GetComponent(UI.Text);
I deleted it and it works now.
Your answer
![](https://koobas.hobune.stream/wayback/20220612154445im_/https://answers.unity.com/themes/thub/images/avi.jpg)