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
0
Question by hubert322 · Nov 23, 2014 at 09:45 AM · pause

Pause the game

So, I use Time.timescale = 0 to pause my game. When I press the pause button, my game stops. Then when I press it again, the game just starts immediately. I want somekind of a 3 seconds pause time between the paused state and the game state. How should I do that? Here's my code:

 public GameObject pauseCanvas;

 public void PauseTheScene ()
 {
     if (Time.timeScale != 0)
     {
         Time.timeScale = 0;
         pauseCanvas.SetActive (true);
     }
     else if (Time.timeScale == 0)
     {
         Time.timeScale = 1;
         pauseCanvas.SetActive (false);
     }
 }

}

Comment
Add comment · Show 2
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 koray1396 · Nov 23, 2014 at 10:06 AM 0
Share

use Invoke() or Coroutine.

avatar image hubert322 · Nov 23, 2014 at 10:23 AM 0
Share

but my time.timescale is 0, so the delay seconds doesn't work right?

4 Replies

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

Answer by VisalDXP · Nov 23, 2014 at 10:38 AM

Edited for Beter Understanding:

 using UnityEngine;
 using System.Collections;
 
 public class GameManager : MonoBehaviour
 {
 
   private IEnumerator _unscaledTimeRoutine;
   public GameObject pauseCanvas;
 
   public void PauseTheScene ()
   {
     if (Time.timeScale != 0)
      {
          Time.timeScale = 0;
          pauseCanvas.SetActive (true);
      }
      else if (Time.timeScale == 0)
      {
          pauseCanvas.SetActive (false);
          _unscaledTimeRoutine = DelayResume(3f);
      }
   }

   //Custom routine
   IEnumerator DelayResume(float sec)
   {
       //set t equal to duration that will delayed
       float t = sec;
       //Keep count down until t <= 0
       while (t > 0)
       {
         t -= Time.unscaledDeltaTime;
         yield return null;
       }
       //when the count down time is out resume game
       Time.timeScale = 1;
       yield return 0;
   }

   void Update()
   {
       if(_unscaledTimeRoutine != null && !_unscaledTimeRoutine.MoveNext())
           _unscaledTimeRoutine = null;

       //The code above can be break down for better understanding
       /*
        //Do we have any routine to run ?
        if(_unscaledTimeRoutine != null)
        {
           //if there is then run the routine and check if it is ended ?
           bool routineEnded = !_unscaledTimeRoutine.MoveNext();
           //if the routine ended then set _unscaledTimeRoutine to null
           if(routineEnded)
              _unscaledTimeRoutine = null;
       */
    }
 }

Comment
Add comment · Show 5 · 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 hubert322 · Nov 23, 2014 at 11:22 AM 0
Share

thx your code is working. Would u $$anonymous$$d explaining the code???

avatar image hubert322 · Nov 23, 2014 at 12:27 PM 0
Share

Also, another occurred. I can't use animations during that 3 seconds.

avatar image VisalDXP · Nov 24, 2014 at 05:36 AM 0
Share

if you're using animator controller, then change the Update $$anonymous$$ode to Unscaled Time. If you choose Update $$anonymous$$ode = Normal then it won't update the animation when the Time.timeScale = 0

avatar image VisalDXP · Nov 24, 2014 at 05:50 AM 0
Share

Normally when the Time.timeScale = 0, StartCouroutine() and Invoke() won't work but the Update function is still being called normally. So in the code that I wrote above, I declared IEnumerator _unscaledTimeRoutine for holding our own custom routine then on Update function I check if _unscaledTimeRoutine is not null (if it is null then it means that there is no routine to run) then call $$anonymous$$oveNext() method of _unscaledTimeRoutine which is return Boolean that deter$$anonymous$$e it could move to next element or not (if it is return false then it means that our routine is ended). So for short, the code in the Update just use to check does it has any routine to run ? if yes then run it and check, has the routine ended yet ? if yes set it to null else keep running.

and one more thing use Time.unscaledDeltaTime because when Time.timeScale = 0 it will make Time.deltaTime = 0 as well.

Sorry for my bad English :) Hope you understand.

avatar image VisalDXP · Nov 24, 2014 at 06:02 AM 0
Share

I edited my code and add some comments to it. hope it help ;)

avatar image
-1

Answer by Jignesh G. · Nov 23, 2014 at 06:17 PM

OnUpdate will continue to execute even if the timescale is zero.

You can add Add Some custom code inside Update() may beto resolve the issue. Hope this will help you.

Ulternatively use iTween With ignoretimescale and pass "comcomplete" method which might execute even if TimeScale is Zero. (This is not tested, but hopefully this will work).

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
avatar image
0

Answer by Kiwasi · Nov 23, 2014 at 10:36 AM

Implement a custom timer based on one of the unscaled values from the time class.

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
avatar image
0

Answer by idurvesh · Nov 24, 2014 at 08:02 AM

Here is what I done in my game,

 OnResumeClicked(){
 //calls coroutine when user clicks on resume button
 StartCoroutine(resumeTimer());
 }
 
 
 IEnumerator resumeCor(){    
 
 float remainTime= Time.realtimeSinceStartup + 3;
 while(remainTime > Time.realtimeSinceStartup){
 
 //shows 3, 2, 1 on UI
 resumeTimerTextButton.text = "" + (int) ((remainTime+1) - Time.realtimeSinceStartup);
 
 yield return null;
 }
 
 Time.timeScale = 1;
     
     }

Hope it helps.

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

31 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Coroutines, WaitForSeconds with function Update? 3 Answers

Main Menu in different scene - pause game 3 Answers

Pause at end of game 2 Answers

Pause Menu problem 0 Answers

Pause Game during Game Center Authenticate? 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