- Home /
How do I reference my timer text??
Hello! I am very new to Unity and C#. I've been getting a reference error in my console and I'm very confused on what to do. Here's my code for the timer:
using System.Collections; using UnityEngine.UI; using UnityEngine;
public class Timer : MonoBehaviour {
public Text timerText;
private float startTime;
private object timer;
// Use this for initialization
void Start () {
startTime = Time.time;
}
// Update is called once per frame
void Update () {
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timer.text = minutes + ":" + seconds;
}
}
Answer by Iarus · Aug 23, 2017 at 09:47 PM
timer
is a private variable that you never set in your code, so it's probably null. timer.text
won't compile because the class object
does not have a text
member.
Remove line 4.
Change line 16 to: timerText.text = minutes + ":" + seconds;
Make sure that you set your timerText
member in the inspector when placing your script on a gameobject.
Your answer
![](https://koobas.hobune.stream/wayback/20220612133409im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Pause & Unpause Different timers 1 Answer
timer problem 1 Answer
Changing light intensity over time via mathf.lerp 2 Answers
How do I assign a prefab reference to a private variable? 1 Answer
Creating a Timer? Halp. 1 Answer