I have an empty object called TimeManager. On the object I have attached the following script. I then dragged the text UI GameObject "MyTime" to the Text Timer field. When the game is not playing everything shows up. When I hit play the object becomes unassigned.
If while game is playing, I can drag the "MyTime" to the Text Timer and then it works fine. I am not sure why it is dropping when I hit play.
public class TimeLeft : MonoBehaviour
{
public float myCoolTimer = 10;
public Text timerText;
public static TimeLeft instance = null;
void Awake ()
{
if (instance == null) {
instance = this;
} else if (instance != this) {
Destroy (this.gameObject);
}
}
void Start ()
{
timerText = GetComponent<Text> ();
}
public void Update ()
{
//Timer
myCoolTimer -= Time.deltaTime;
timerText.text = myCoolTimer.ToString ("f2");
}
Note: I need the singleton because I use it in other scripts.