OnScreen Timer causing serious lag

Morning everyone… i’m having an issue that i can’t seem to figure out a better solution for. We have a timer in our game thats represented by a clock on screen that uses the code below. When the game is played in normal mode (aka non-timed) the framerate is great… but when the same exact level is played in timed mode we get very visible lag. The levels are identical except for the timer. Here’s the code we’re using…

	string PrintTimer()
	{
		string TimeStr = Minutes().ToString();
		TimeStr+=":";
		int sec = Seconds();
		if (sec < 10)
		TimeStr+="0";
		TimeStr += sec.ToString();
		return(TimeStr);
	}
	

	void Update () 
	{
		if(isTimedOn == true  Paused == false)
		{
			if(Timer != null)
				Timer.Text = PrintTimer();
			if(Timer !=null  TimeRemaining() <= 60  Timer.gameObject.GetComponent<GroupFader>().bFadeLoop == false  flashCount != 3)
			{
				Timer.gameObject.renderer.material.SetColor("_Color", Color.red);
				Timer.gameObject.GetComponent<GroupFader>().bFadeLoop = true;
				Timer.gameObject.GetComponent<GroupFader>().FadeOut();
			}
			if (Timer !=null  Timer.gameObject.GetComponent<GroupFader>().bFadeLoop == true  flashCount == 3)
			{
				Timer.gameObject.GetComponent<GroupFader>().bFadeLoop = false;
				//Timer.gameObject.GetComponent<GroupFader>().FadeIn();
				Timer.gameObject.GetComponent<ShowHide>().Show(true);
			}
			if(Timer !=null  TimeRemaining() <= 15  Timer.gameObject.GetComponent<GroupFader>().bFadeLoop == false)
			{
				Timer.gameObject.GetComponent<GroupFader>().bFadeLoop = true;
				Timer.gameObject.GetComponent<GroupFader>().FadeOut();
			}
		
			if(Timer !=null  TimeRemaining() <= 0)
			{
				Debug.Log("times up!");
				Timer.Text = "";
				TimeModeFail.Show(true);
			}
		}
	}
	
	public void addFlashCount()
	{
		flashCount = flashCount + 1;	
	}
	public int Minutes()
	{
		return (TimeRemaining()/60);
	}
	
	public int Seconds()
	{
		return (TimeRemaining() % 60);
	}
	
	public int TimeRemaining()
	{
		int curTime = (int) Time.time;
		int TimeElapsed = curTime - (LevelStartTime + pauseTime);
		int TimeLeft = LevelDuration - TimeElapsed;
		if(addToTime != 0)
		{
			addToTime = 0;
			return(TimeLeft + addToTime);
		}
		else if (subFromTime != 0)
		{
			return (TimeLeft - subFromTime);	
		}
		else
		{
			return (TimeLeft);
		}
	}

Curious if anyone out there has any advice on optimizing the above code or better yet a simpler implementation of a timer on screen (that shows in minutes and seconds). Right now the slowdown is significant. it’s not constant… more like periodic spots of lag that happen quite often.

Cheers and thanks for the help!
Bryan

You’re doing GetComponent calls constantly in Update, for one thing, but I’d advise getting rid of Update and using a coroutine, which will clean up all that if/then logic. There’s no reason to have all that stuff going on every frame, since apparently it only changes once every second. Also doing string concatenations is not a good idea, and you don’t need to manually format things, you can just use the built-in string formatting.

–Eric

@Eric,

Heya eric, thanks for the response. One thing i didn’t note was that i completely commented out pretty much all the update function and left only this one bit…

		if(isTimedOn == true  Paused == false)
		{
			//FOR FUTURE REFERENCE --> This is how you get and use a different component of a defined type. for instance, i've setup Timer as spritetext but i'm pulling in the show/hide as well.
			if(Timer != null)
				Timer.Text = PrintTimer();
}

and it was still ultra laggy…

ok so i changed up a bit how we’re handling the timer… and it still seems to be pretty hitchie…

void Start()
{
	LevelStartTime = Time.time;
	InvokeRepeating("TimerUpdate", 0.0f, 1.0f);
}

	private void TimerUpdate()
	{
		if(isTimedOn == true  Paused == false)
		{
			float curTime = Time.time;
			float TimeElapsed = curTime - (LevelStartTime + pauseTime);
			float TimeLeft = LevelDuration - TimeElapsed;
			float minutes = Mathf.FloorToInt(TimeLeft / 60);
			float seconds = TimeLeft % 60;
	                timerValue = string.Format("{0:00}:{1:00}", minutes, seconds);
			Timer.Text = timerValue;
		}
	}

And thats it. With that running i’m still seeing significant slowdowns. Is that not the right direction to go? I’ve removed everything completely from the update function…

WEll good news… its not the timer. Apparently something else is running during timed mode that causes the lag… thanks for the help eric.