Hello!
I write game with forward timer in format 00:00:00 (min:sec:milisec)
But on first execution of timer ticking i have annoying lag in ~1 sec. This happens only on fisrt level. On second (and other levels) everything just fine. More over: if i pressed “replay” button (without closing game) there is no any lag. BUT if i close my game and start again this lag appearse again. After long time of debugging i figure out, than lag in this row:
gtScoreCount.text = string.Format("{0:00}:{1:00}:{2:00}", minutesDisplay, secondsDisplay, nanoDisplay);
I tried move this to InvokeRepeating() (instead of Update() ), tried change GUIText on Uniti.UI.Text. but have no any positive result. This driving me crazy. Can u advice me what i doing wrong? . Here is my full code now:
public class TheTimer : MonoBehaviour {
public static float timerGlobal; //global levels timer
public GUIText gtScoreCount;
public bool timeStarted;
public float timer; //local level timer
private float timerDisplay;
private float minutesDisplay = 0;
private float secondsDisplay = 0;
private float nanoDisplay = 0;
void ResfreshTimer(){
if (timeStarted) {
// IF COMMENT THIS STRING EVERYTHING GO FINE WITHOUT LAG
gtScoreCount.text = string.Format("{0:00}:{1:00}:{2:00}", minutesDisplay, secondsDisplay, nanoDisplay);
}
}
void Update(){
if (timeStarted) { //global ticking timer
timer += Time.deltaTime;
timerDisplay = timer + timerGlobal;
minutesDisplay = Mathf.Floor(timerDisplay / 60);//.ToString("00");
secondsDisplay = Mathf.Floor(timerDisplay % 60);//.ToString("00");
nanoDisplay = Mathf.Floor(timerDisplay * 100 % 100);//.ToString("00");
}
void Start () {
timeStarted = true;
InvokeRepeating("ResfreshTimer",7.0F,0.05F);
}
}