Question by
FlameCrafter · Apr 17, 2016 at 03:51 AM ·
uitexttimer
What do you insert in the "" area in this timer? (00:00)
here is what I have so far
public text timerText;
float myTimer = 90;
void Update ()
{
myTimer -= Time.deltaTime;
SetTimerText();
if (myTimer <= 0)
{
}
}
void SetTimerText()
{
timerText.text = "" + count.ToString();
}
}
Comment
Answer by jmparavicini · Apr 17, 2016 at 08:02 AM
Ok first of all i think you want to make a timer with this format: minutes:seconds right? In your program there are various errors the most obvious seems to me that count is never defined. I wrote a short script here to help you.
using UnityEngine;
using System.Collections;
//Needed to access Text component
using UnityEngine.UI;
public class Timer : MonoBehaviour
{
//you need a Text component attached to the gameobject
Text timerText;
public float myTimer = 90.0f;
void Start()
{
//Initialize the text component
timerText = GetComponent<Text> ();
}
void Update ()
{
SetTimerText();
//Only decrease the timer when it is greater than 0 otherwise make sure that the timer is is not going to the negative part
if (myTimer > 0.0f)
{
myTimer -= Time.deltaTime;
} else
myTimer = 0.0f;
}
void SetTimerText()
{
timerText.text = GetMinutes ().ToString ("00") + ":" + GetSeconds ().ToString ("00");;
}
float GetMinutes()
{
//return the greater integer when dividing -> 5/3 = 2
return Mathf.Floor (myTimer / 60);
}
float GetSeconds()
{
//Modulus(remainder) of timer -> the remainder when dividing timer with 60 --> 125/60 = 2 + remainder 5
return myTimer % 60;
}
}
I hope it helps you -Jorge