- Home /
Timer lag at GUIText drawing.
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);
     }
 
 }
Answer by silmarill · Aug 11, 2015 at 04:17 AM
Holy waka vaca. I solved this. I'll answer to myself. Maybe somebody this helps.
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.
Your answer
 
 
             Follow this Question
Related Questions
Showing and Hiding guiText 1 Answer
Update not being called every frame 2 Answers
For loop resetting itself, but needs to stop 2 Answers
Is an object with an update function possible? (C#) 1 Answer
Set guiText 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                