- Home /
Problem with Pause and WaitForSeconds
I wanted to stop my game when "P" is pressed and continue it when "P" is pressed again - but at least o,1 seconds later. For this I used the following script:
using UnityEngine;
using System.Collections;
public bool Pause = false;
public bool PauseUmstellbar = true;
void Update ()
{
if (Pause) {
Time.timeScale = 0.00001f;
} else {
Time.timeScale = 1;
}
if (Input.GetKey (KeyCode.P)) {
if(PauseUmstellbar){
PauseUmstellbar=false;
StartCoroutine(PauseUmstellen());
if (Pause) {
Pause = false;
} else {
Pause = true;
}
}
}
}
IEnumerator PauseUmstellen(){
if (Pause) {
yield return new WaitForSeconds (0.000001f);
PauseUmstellbar = true;
} else {
yield return new WaitForSeconds (0.1f);
PauseUmstellbar = true;
}
}
It works but if its Paused it never will be de-paused again. What can I do?
I suspect if you use Get$$anonymous$$eyUp rather than Get$$anonymous$$ey, you will eli$$anonymous$$ate the need for a delay check. Get$$anonymous$$ey returns true whenever the key his down. But, Get$$anonymous$$eyUp and Get$$anonymous$$eyDown will only return true on the one cycle the key is released/depressed.
The way you have it, even if working properly, the game would pause and then unpause every 0.1 seconds, while the key is down.
Answer by maccabbe · Apr 05, 2015 at 05:07 PM
There are a few problems with the way you have written the script. The major problem is that you change pause after you start the coroutine. This causes PauseUmstellen to use wait for 0.1f seconds when pausing. Then you change the timescale to 0.00001 which means your program will allow new pause commands in about 3 hours.
However if you try to move the pause command before the coroutine starts, you wait for 0.0000001f seconds at normal time until the next Update changes the timescale but by then 0.0000001f seconds have passed. To fix this error you should immediately change the timescale upon pausing (it is also more efficent to assign timescale one when it changes).
Here is an example of code (I cleaned it up 2 parts and changed the delay to 0.5f seconds to help me debug it).
public bool Pause=false;
public bool PauseUmstellbar=true;
void Update() {
if(Input.GetKey(KeyCode.P)) {
if(PauseUmstellbar) {
PauseUmstellbar=false;
Pause=!Pause;
if(Pause) {
Time.timeScale=0.00001f;
}
else {
Time.timeScale=1;
}
StartCoroutine(PauseUmstellen());
}
}
}
IEnumerator PauseUmstellen() {
if(Pause) {
yield return new WaitForSeconds(0.000005f);
}
else {
yield return new WaitForSeconds(0.5f);
}
PauseUmstellbar=true;
}
Thanks a lot. It works! There is just one problem: I am not able to stop the movement of the camera. For this I set the content of the Update-void in the $$anonymous$$ouselook Script like this: void Update () { GameObject thePlayer = GameObject.Find ("First Person Controller"); PauseScript PlayerScript = thePlayer.GetComponent (); if (PlayerScript.Pause == false) { ... } }
He sais at the Line with PauseScript PlayerScript...:
Der Typ oder Namespacename PlayerScript konnte nicht gefunden werden. (Fehlt eine Using-Direktive oder ein Assemblyverweis?) = The type or namespacename PlayerScript couldnt be found. (Does a Using-directive or an Assemblyreference miss?)
*PauseScript PlayerScript = thePlayer.GetComponent();
is in the line. I forgot it in my comment.
Answer by Deadcow_ · Apr 05, 2015 at 05:08 PM
You may put whole pause logic inside the coroutine. It can be look like this:
IEnumerator PauseLogic(){
while (true) {
while (!Input.GetKeyDown(KeyCode.P)) yield return null;
Pause = !Pause;
Time.timeScale = (Pause) ? 0.00001f : 1;
float elapsed = 0;
while (elapsed < 1) {
elapsed += Time.deltaTime;
yield return null;
}
}}
Thats should work, you just need to enable this logic by starting coroutine in Start function
But why do you want to skip one second after the pause? It may be all you need is to skip one frame and wait for the next Input.GetKeyDown(KeyCode.P)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Pause Menu Not Pausing 3 Answers
Pause menu not working.. 1 Answer
Why does this bool get reset? 1 Answer