- Home /
Time.deltaTime not set to 0 if Time.timeScale set to 0 too early!
Can someone confirm this bug exists or suggest where to look for my mistake?
Notice that the problem also happens if you try to set timeScale to 0 in Awake(). (which in this case would be the sensible place to put it.)
void Update() { //if(Time.frameCount < 2) //deltaTime becomes 0.02, not zero. //same problem if put in Awake();
if(Time.frameCount == 2) //works as expected.
Pause();
if(Input.GetKeyDown(KeyCode.Space))
Pause();
else if (Input.GetKeyUp(KeyCode.Space))
Play();
Debug.Log("Delta"+Time.deltaTime);
}
void Pause () {
Time.timeScale = 0;
AudioListener.pause = true;
}
void Play () {
Time.timeScale = 1;
AudioListener.pause = false;
}
}
Answer by Jaap Kreijkamp · Mar 10, 2010 at 03:25 AM
Time.deltaTime
is the time between previous and the current frame, so setting the timeScale within an Update doesn't change the value for deltaTime for the Updates called in the current frame.
While this is true, in subsequent calls to Update many frames later, deltaTime should be 0 and I still see it as a s$$anonymous$$dy 0.02 indefinitely (until timeScale is changed again some time after frame count is >= 2). Let me know if you do run it and get similar results.
So if I understan it correctly, it works well after frame 2 (deltaTime == 0) but wrong before frame 2 (deltaTime == 0.02)? Apparently Unity's code does force Time.timeScale to 1 in first frame or so. As I've used timeScale == 0 without problems I would suspect it only occurs on first frames after startup, a cheap hack around would be having an startup scene, that waits 2 frames and then loads the real stuff.
That is correct. I just ended up waiting for the two frames and starting my scene with no sounds to prevent any audio noises in those first frames.
If it is correct, mark your question as answered (by this answer).
Answer by Gareth Rees · Jun 12, 2010 at 08:21 PM
I've seen this bug too (Unity 2.6.1f3): if I set Time.timeScale
in an Awake()
call before the first frame, or in an Update()
call in the first frame, then Time.deltaTime
is not properly adjusted. (Animations do stop, though: I guess they interrogate Time.timeScale
directly).
Answer by arioch82 · Dec 01, 2010 at 11:47 AM
I've bumped into this too... setting Time.timeScale=0.0f in an Awake in the first scene won't change the deltaTime (always 0.02f). this is really annoying...
Answer by starkos · May 15, 2012 at 03:13 PM
Here's a way to work around it:
function Awake() {
StartCoroutine("Pause");
}
function Pause() {
yield WaitForEndOfFrame();
Time.timeScale = 0.0f;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612102200im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Execute code every x seconds with Update() 4 Answers
speed change in standalone compared to editor? 1 Answer
I can't get framerate independence to work... 1 Answer
Different behavior between editor and standalone changing time step 0 Answers
Lots of !IsFinite() Errors and FPS crawling to a halt - How to debug? 1 Answer