- Home /
My timer won't work?
So I am making a game where when the timer hits one min you fail and have to restart. I am working with my friend on this he's pretty expeirenced and I am a beginner. This code is not working. using
System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Timer : MonoBehaviour { Text text; float theTime; public float speed = 1f;
Text MaxTime;
Text CurrentTime;
// Start is called before the first frame update
void Start()
{
text = GetComponent<Text>();
CurrentTime = GetComponent<Text>();
}
//Update is called once per frame
void Update()
{
theTime += Time.deltaTime * speed;
string hours = Mathf.Floor((theTime % 216000) / 3600).ToString("00");
string minutes = Mathf.Floor((theTime % 3600) / 60).ToString("00");
string seconds = (theTime % 60).ToString("00");
text.text = hours + ":" + minutes + ":" + seconds;
if (CurrentTime.text = "01:00:00".ToString)
{
}
}
}
I can't help because I'm the friend you are talking about!
Answer by rfry336 · Feb 02, 2019 at 06:47 PM
I made a test game with a timer on the screen. The original code does work it was but the time was set to 1 hour.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Timer : MonoBehaviour
{
[SerializeField] float speed = 1f;
Text text;
Text MaxTime;
Text CurrentTime;
float theTime;
// Start is called before the first frame update
void Start()
{
text = GetComponent<Text>();
CurrentTime = GetComponent<Text>();
}
//Update is called once per frame
void Update()
{
theTime += Time.deltaTime * speed;
string hours = Mathf.Floor((theTime % 216000) / 3600).ToString("00");
string minutes = Mathf.Floor((theTime % 3600) / 60).ToString("00");
string seconds = (theTime % 60).ToString("00");
text.text = hours + ":" + minutes + ":" + seconds;
if (CurrentTime.text == "00:01:00".ToString())//This time was set to 1 hour.
{
//Do something here when the time is up.
Debug.Log("Time Is Up");
}
}
}
By answer done by test above
So the main change is ?
if (CurrentTime.text == "01:00:00")
{
}
to
if (CurrentTime.text == "00:01:00")
{
}
Yes?
FYI:
if (Equals(CurrentTime.text, "00:01:00"))
{
}
Above is same as Top, but with this ! goes at front NOT in an != format. (CurrentTime.text != "00:01:00") to do a Not If equals statement.
ToString isn't needed as it already is a string to begin with. Anything inside "" is a string...Not to mention it is missing () at the end of it. $$anonymous$$inda supersized that, that didn't make Unity whine at you!*
Answer by andrew-lukasik · Feb 19, 2019 at 10:26 AM
I answered this question once before here: How to code timers without creating new strings every frame
NOTE: this solution is probably for more advanced programmers (at least because of that lambda/anonymous function syntax). Nevertheless you can still look up some useful ideas from there.
Doesn't seem That complicated, thanks for the Link. Someone should close this question though.
Answer by toddisarockstar · Feb 02, 2019 at 05:02 AM
I am looking at your code and it hurts my eyes to see all that division. division is much harder on a CPU than multiplication and you are calling it all once per frame! anyways here is a simpler way that works
public int min;
public int sec;
public int hour;
public string show="00:00:00";
public float f;
void Update () {
f += Time.deltaTime;
if (f > 1) {f-=1;
//one second has passed so now we do stuff
sec++;if(sec==60){sec=0;min++;if(min==60){min=0;hour++;}}
show = format (hour)+":"+format(min)+":"+format(sec);
//check for time and update your display here
print (show);
}}
public string format (int z){
string r = "" + z;
while(r.Length<2){r="0"+r;}
return r;
}
1000 divisions per frame may be an issue. Few is probably just complete cpu-pennies when compared to object (string) allocations every frame