I want my Unity game to start with Time.timeScale = 0f becuase it starts with a main menu that is in the same scene as the game. I tried to put Time.timeScale = 0f in Awake() but that did not work as expected. Putting it in Awake() paused it almost immediately but somehow the update funtions run at least once because some objects moved a little before the game paused. How do I pause it immediately when the game starts?
-
\$\begingroup\$ As a side note it may be nicer to have the game running idle animations in the background instead of being completely paused. Keeping the screen moving is a key part of user engagement. \$\endgroup\$– Luke BriggsCommented Feb 10, 2020 at 13:03
-
1\$\begingroup\$ Thanks for the feedback, but I have already thought about that. I have animations playing anyway by setting the animators update mode to unscaled time. \$\endgroup\$– RidertvisCommented Feb 10, 2020 at 13:21
-
\$\begingroup\$ Also check your Script execution order. Edit->project settings->script execution order. Make sure your code is being called first. Setting timeScale to 0f, is the correct way. \$\endgroup\$– Joe SwindellCommented Feb 10, 2020 at 13:49
2 Answers
You can specify the setting on launch using the Time section of the project settings (Edit>Project Settings>Time).
Note that Update
function is not affected by timeScale (Though it affects all measured delta times in the Time class, except for fixedDeltaTime). So, you should build some logic to tackle this. On the start of the Update
body, add if (Time.timeScale == 0f) return;
. This will effectively prevent Update
from running while Time.timeScale is 0.
More info on Time.timeScale from the docs.