Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 salgado18 · Oct 18, 2013 at 03:18 PM · timepausetimescaleinvokerepeatingpause game

Pausing an Invoke for a time

I'm coding a game that's 70% done, and now I'm doing the pause system. All loops and updates have an if(isPaused) to them, and works very nice.

However, in many places there is an Invoke("Something", delay), like on weapon cooldowns and unit respawning. But when I pause, I can't use Time.timeScale = 0 because the pause menu animation depends on it.

Does anyone know a way to pause or delay an Invoke until the game is unpaused, without using the timeScale or reimplementing the Invoke system? I'm really running out of ideas here. :/

Comment
Add comment · Show 5
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 Jamora · Oct 18, 2013 at 03:18 PM 1
Share

There are a lot of questions on UA about pausing your game. You should search the site before asking.

http://answers.unity3d.com/questions/7544/How-do-I-pause-my-game.html

$$anonymous$$ust be able to help you because it has so many correct answers.

avatar image mattmanj17 · Oct 18, 2013 at 03:50 PM 3
Share

The link provided in the last comment is a good one, you should check it out. But in response to your specific case, I would use a coroutine ins$$anonymous$$d of invoke(). With a coroutine, you could decrease a counter every frame, and then call the function you wanted when that counter reached zero. The salient different is that you can make it so the counter only goes down when the game is un-paused, by checking your isPaused var. something like the code below should work

 IEnumerator my_function_wraper(float delay){
     while(true){
         if(delay>0){
             if(unPaused)
                 delay-=time.deltatime;
             }
             yeild return;
             continue;
         }
         else{
             myfunction();
             yeild break;
         }
     }
 }

Disclaimer: I just typed in this code right now, so it may have some errors, but the general idea is there. If you are new to coroutines, check out this page

http://docs.unity3d.com/Documentation/$$anonymous$$anual/Coroutines.html

If you have to make this change for many different functions, you could make one method (lets call it pause_safe_invoke) that worked like the one above, but then use delegates so you can change what function gets called. Good page on delegates here

http://msdn.microsoft.com/en-us/library/vstudio/ms173172.aspx

avatar image robertbu · Oct 18, 2013 at 04:01 PM 0
Share

This is an interesting question. None of the answers at the link @Jamora provided address it, nor have I seen a fix for this specific problem. I don't know how you are doing your menu animation, but one possibility is to recode it to use Time.realTimeSinceStartup rather than deltaTime. This would allow you to set Time.timeScale to 0.

avatar image zombience · Oct 18, 2013 at 04:50 PM 0
Share

@mattmanj17 seems to have the more elegant solution. if you didn't want to go through and change all of your code, you could have a "pause" call CancelInvoke("$$anonymous$$ethod") which will stop your invokes.

Then you could add in coroutines from mattman17's solution to count how much time had passed since the invoke was called, but before it executed, and on unpause call Invoke("$$anonymous$$ethod", remainingDelayTime).

So, this is a really sloppy way of doing it. I can't really recommend it. But it's possible that you might be able to just add the little bit of functionaly to your code, rather than completely rewriting everything. It's also entirely possible that the Invoke ti$$anonymous$$g might differ from the coroutine ti$$anonymous$$g. So it depends on how precise your ti$$anonymous$$g needs to be.

avatar image salgado18 · Oct 18, 2013 at 09:34 PM 0
Share

I've seen the link, it really has many good answers. But I know many of them, and the best one involves recoding the menu system. Not that it would be wrong, but it would be creating work for someone else other than me (the guy who built the entire menu system).

mattmanj17's solution is a good one, but creating a corroutine in every script with an Invoke could be too heavy, especially since the game is already CPU intensive, so it needs testing. I like the idea of doing the same idea globally, managing all Invokes, but could be too much work (and prone to errors).

I think I will make the public message OnPause() and OnUnpause(), like the link said, and save the starting and pause times of Invoke methods, and calculating the delay if necessary. Once I get it running (or any other system), I'll post as an answer.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by devang024 · Jun 07, 2015 at 09:28 AM

Late to the party, but here's the after party stuff.

public void prepareForPlay() {

     if (isPaused == false) {
         /*Game's meat*/
     }
     else 
     {
         StartCoroutine(asyncPrepareForPlay());
     }
 }

 IEnumerator asyncPrepareForPlay()
 {
     yield return !isPaused;
     prepareForPlay ();
 }

and this "prepareForPlay" function is called with Invoke("prepareForPlay",5.0f) somewhere, so when prepareforplay is called and game is paused(isPaused==true), it will go to coroutine and will not call that function until isPaused is FALSE. I 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
avatar image
0

Answer by FreshTofu · May 09, 2018 at 12:55 AM

You could also set your animation to unscaled time in the inspector window under the animator component. Im late, however this could be for anyone who may be reading this in the future. :)

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

19 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

Related Questions

How to Pause game 1 Answer

Can't really pause my game 1 Answer

time.unscaled time help please? 1 Answer

Pause game that not using deltatime for movment 1 Answer

Pause button 2 Answers


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