Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by melwei · Apr 05, 2015 at 03:16 PM · c#pausepausing

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Apr 05, 2015 at 04:37 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image melwei · Apr 05, 2015 at 06:26 PM 0
Share

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?)

avatar image melwei · Apr 06, 2015 at 07:43 AM 0
Share

*PauseScript PlayerScript = thePlayer.GetComponent();

is in the line. I forgot it in my comment.

avatar image
0
Wiki

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges