I was trying to create a loop to slow down my game but this doesn't work. please advise

why didn’t this work?

currentTime = Time.time; //pause for wait time
while(Time.time < currentTime + 2) { };

then I also tried this to make an infinite loop until the space bar is hit.

void Update()
{

while (!Input.GetKeyDown(“space”))
{
PauseGame();
}

ResumeGame();

}

void PauseGame()
{
Time.timeScale = 0;
}
void ResumeGame()
{
Time.timeScale = 1;
}

It didnt work because you didnt use code tags. j/k :smile: Please use code tags.

In essence, you have an infinite loop already at your disposal. Update(){} Ahh, but you want to slow it down… i see. This doesnt really slow it down, it straigh up stops it. But adjust values as you see fit once you get it working.

        [SerializeField] KeyCode keyPause;
        bool IsPaused;


        private void Update()
        {
            if (Input.GetKeyDown(keyPause))
            {
                IsPaused = !IsPaused;
                if (IsPaused) Time.timeScale = 0.0f;
                else Time.timeScale = 1.0f;
            }
        }

So you can’t just copy paste this directly, but its kind of the idea i think you were presenting in attempt #2. Anyways, this is fairly simple stuff. A few good tutorials and you should have 'er down. Gl.

2 Likes

Unity will lock up 100% of the time EVERY millisecond your scripting code is running.

Nothing will render, no input will be processed, no Debug.Log() will come out, no GameObjects or transforms will appear to update.

Absolutely NOTHING will happen… until your code either:

  • returns from whatever function it is running

  • yields from whatever coroutine it is running

As long as your code is looping, Unity isn’t going to do even a single frame of change. Nothing.

No exceptions.

“Yield early, yield often, yield like your game depends on it… it does!” - Kurt Dekker

Don’t do infinite loops. As explained this just locks down execution. You want to be smarter than that.
Playing with frame rate must be done on a meta level, because it’s under control of Unity itself.
You can also rely on Time.timeScale like Homicide suggests, because if you rely on Time.deltaTime or Time.time anywhere else in your code, it’ll get affected by Time.timeScale.

That means you can just as easily implement your own Time tracking logic. For example what do you do if you want the animations (or game) to keep running while you’re in the pause menu? You introduce your own Time logic, with trackers separated.

1 Like