Use Coroutines independent of timeScale, Unity 3D

by on under Code snippets
1 minute read

I wanted to put a 3,2,1 counter before my game starts.
I have set the timeScale to 0 in Start function and then I realized that WaitForSeconds is based on timeScale.
The workaround is from unity answers.

IEnumerator Counter(){
	while(true){
		float pauseEndTime = Time.realtimeSinceStartup + 1f;
		while (Time.realtimeSinceStartup < pauseEndTime){
			yield return 0;
		}
		Debug.Log(timer);
		timer-=1;
		if(timer == 0){
			Debug.Log ("Start");
			Time.timeScale = 1f;
			StopCoroutine("Counter");
		}
	}
}
Unity 3D, coroutine, time, timeScale, WaitForSeconds, code snippet
comments powered by Disqus