Reputation: 3
I have problem with reaching the else statement in Update
method and I don't know how to make this clock stop. S
The startTime
is in seconds. If you make it 90.0f
you will have 1.30 minute. The problem is I need to stop this clock when reaches the 0:0.0
.
public Text TimerText;
private float startTime = 3.0f;
private bool start = false;
private float _time;
private float _minutes, _seconds;
// Use this for initialization
void Start ()
{
start = false;
startTime = 3.0f;
}
// Update is called once per frame
void Update ()
{
// if (start)
// return;
if (startTime > 0.0f)
{
_time = startTime - Time.time; // ammount of time since the time has started
_minutes = (int)_time / 60;
_seconds = _time % 60;
TimerText.text = _minutes + ":" + _seconds.ToString("f1");
}
else
Debug.Log("we are here");
}
private void CheckGameOver()
{
Debug.Log("gameover");
}
public void StartTime()
{
TimerText.color = Color.black;
start = true;
}
Upvotes: 0
Views: 91
Reputation: 125305
It is recommended to use Time.deltaTime
like Pawel did in his answer.
I know that this problem is already solved but just in-case you want to know why your code is not working, that's because when you did if (startTime > 0.0f)
, you are supposed to be decrementing startTime
in the if statement. If you don't, startTime>0
will always be true
which means that the if statement will run forever.
You can still fix this by simply replacing your if (startTime > 0.0f)
with if (Time.time < startTime)
. You don't need to decrement anymore but it will work.
Upvotes: 0
Reputation: 630
Use Time.deltaTime
instead of Time.time
and change:
if (_time> 0.0f)
{...}
Add _time = startTime
into Start()
Upvotes: 3