I am trying to do when i run the game it wait 3s before it starts in Unity. I put all gameobjetc in a empty object with code:
[SerializeField]
private float seconds = 3f;
private void Start()
{
StartCoroutine(Wait());
}
private IEnumerator Wait()
{
Time.timeScale = 0f;
yield return new WaitForSeconds(seconds);
Time.timeScale = 1f;
}
And when I start the game its just frozen nothing is counting down.
CodePudding user response:
Because WaitForSeconds
corresponds to the time in the game and follows Time.scale
, So when you set Time.Scale
to 0
, no time passes. Use WaitForSecondsRealtime
to resolve this issue, This method works independently of Time.Scale
, and if you use Slow-Motion or Freeze effects in your game, consider this one:
yield return new WaitForSecondsRealtime(seconds);
CodePudding user response:
You should be able to solve your issue by removing the Time.timeScale = 0f;
code. Let me explain why.
If you decrease the timescale to say 0.5, you are increasing your time by two. So your 3 / 0.5 = 6 seconds
.
When you are setting your timescale to 0 you are essentially pausing your app because you are making your app wait infinitely.
So if you remove the timeScales like this it should work fine:
private IEnumerator Wait()
{
yield return new WaitForSeconds(seconds);
}