Timer lag in beginning

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);
    }

}

You sure it’s not just lag from loading your assets for the game? Do you have a loading screen before you begin the game?

Holy waka vaca. Problem solved. This was really deep. And it was… font renderer problem.
My FONT was imported as “dynamic” and on 1st lvl I forced GUIText to render all sybols from 1 to 9 in one frame (milisec timer). I solved this by putting gameobject with GUIText " 123456789:’ ", fontsize 30, color - white (used in my game - it is important) on hidden (unreached by camera X,Y) area of my Logo-scene.

Now it works perfectly. Maybe somebody this helps.

1 Like