Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Timer lag in beginning

Discussion in 'Scripting' started by silmarill, Aug 10, 2015.

  1. silmarill

    silmarill

    Joined:
    Jul 13, 2013
    Posts:
    10
    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:
    Code (CSharp):
    1. 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:

    Code (CSharp):
    1. public class TheTimer : MonoBehaviour {
    2.  
    3.     public static float timerGlobal;  //global levels timer
    4.     public GUIText gtScoreCount;
    5.     public bool timeStarted;
    6.     public  float timer;  //local level timer
    7.     private float timerDisplay;
    8.     private float minutesDisplay = 0;
    9.     private float secondsDisplay = 0;
    10.     private float nanoDisplay    = 0;
    11.  
    12.  
    13.     void ResfreshTimer(){
    14.  
    15.         if (timeStarted) {
    16.             // IF COMMENT THIS STRING EVERYTHING GO FINE WITHOUT LAG
    17.             gtScoreCount.text = string.Format("{0:00}:{1:00}:{2:00}", minutesDisplay, secondsDisplay, nanoDisplay);
    18.         }
    19.     }
    20.  
    21.     void Update(){
    22.         if (timeStarted) { //global ticking timer
    23.              timer += Time.deltaTime;
    24.             timerDisplay = timer + timerGlobal;
    25.             minutesDisplay = Mathf.Floor(timerDisplay / 60);//.ToString("00");
    26.             secondsDisplay = Mathf.Floor(timerDisplay % 60);//.ToString("00");
    27.             nanoDisplay    = Mathf.Floor(timerDisplay * 100 % 100);//.ToString("00");
    28.     }
    29.  
    30.     void Start () {
    31.         timeStarted = true;
    32.         InvokeRepeating("ResfreshTimer",7.0F,0.05F);
    33.     }
    34.  
    35. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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?
     
  3. silmarill

    silmarill

    Joined:
    Jul 13, 2013
    Posts:
    10
    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.
     
    Last edited: Aug 11, 2015
    GroZZleR likes this.