Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
10
Question by Numli · Sep 08, 2014 at 03:03 PM · javascriptcoroutinetimescalegui.buttonslide

Make a coroutine run when Time.timeScale = 0;

Hi Guys,

I have been working on a side scroller type game and have alot of my code run in the fixed update method, I currently pause the game using Time.timescale = 0; which works perfectly, however I am adding the in game menu at the moment and would like the buttons to 'slide' onto the screen instead of just appear, I have written the following coroutine to make this happen

 function Slide() {
     for (var i: int = -10; i < 0; i++) {
         ofset = 10 * i;
         yield WaitForSeconds(0.1f);
     }
     ofset = 0;
 }

with this as my button code

 function gamePausedMenu(windowID:int){
     //Restart Button
         GUI.Button(Rect(Screen.width/2 - scoreSize/2 - ofset, Screen.height/2 - (scoreSize/3)/2, scoreSize,scoreSize/3),"Restart", leftTop);
     //Menu Button
         GUI.Button(Rect(Screen.width/2 - scoreSize/2 + ofset, Screen.height/2 + (scoreSize/3), scoreSize,scoreSize/3),"Menu", rightTop);
 
 }


Now this works perfectly however it doesn't run at all while Time.timescale = 0, which I do on pause of course, is there any work around to this, so that I can force my that coroutine to run regardless of timescale?

Thanks in Advance!!

Comment
Add comment
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

3 Replies

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

Answer by Bunny83 · Oct 27, 2016 at 10:44 PM

Unity now has a special "WaitForSeconds" called WaitForSecondsRealtime which does the same as WaitForSeconds but is not affected by Time.timeScale.

This update renders the other answers out-of-date.

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 sdclark79 · Oct 15, 2017 at 07:47 PM 0
Share

This is the best answer. Thank you!

avatar image Ricardo-Martins · Jun 27, 2018 at 03:42 AM 0
Share

Real Best Answer ... THX man :D

avatar image Ellernate · Aug 07, 2018 at 11:51 PM 1
Share

Just a tip for people who convert existing "WaitForSeconds" to " WaitForSecondsRealtime":

Unlike WaitForSeconds, WaitForSecondsRealtime doesn't work correctly when reused. It won't throw any errors, however it will only yield for 1 frame ins$$anonymous$$d of the specified time. This is common if you're caching one instance for loops ins$$anonymous$$d of generating new copies.

avatar image Bunny83 Ellernate · Aug 08, 2018 at 07:19 AM 0
Share

Yes, this is because unlike WaitForSeconds WaitForSecondsRealtime is implemented as "CustomYieldInstruction". While WaitForSeconds is essentially just a container for the desired wait time and the actual waiting logic is implemented in native code, WaitForSecondsRealtime actually contains the state and logic for the waiting. So once the time is expired it will stay expired.


However you can create a "garbage free" solution like this:

 public class WaitForSecondsRT : CustomYieldInstruction
 {
     float m_Time;
     public override bool keepWaiting
     {
         get { return (m_Time -= Time.unscaledDeltaTime) > 0;}
     }
     public WaitForSecondsRT(float aWaitTime)
     {
         m_Time = aWaitTime;
     }
     public WaitForSecondsRT NewTime(float aTime)
     {
         m_Time = aTime;
         return this:
     }
 }

This can be reused when you use the "NewTime" method.

Of course you have to ensure to never use the same instance in two coroutines at the same time. It would be best practise to create the instance locally inside the coroutine and just use it inside the coroutine.

 IEnumerator $$anonymous$$yCoroutine()
 {
     WaitForSecondsRT wait = new WaitForSecondsRT(1);
     while (true)
     {
         // ...
         yield return wait.NewTime(0.5f);
         //  ...
     }
 }

avatar image GUE12 · Oct 24, 2018 at 10:05 AM 0
Share

The best Answer!! THX!

avatar image
16

Answer by brunopava · Apr 16, 2015 at 12:34 PM

So, in the link below the guy figured it out in an elegant solution that is also re-usable.

 public static class CoroutineUtilities 
 {
     public static IEnumerator WaitForRealTime(float delay){
         while(true){
             float pauseEndTime = Time.realtimeSinceStartup + delay;
             while (Time.realtimeSinceStartup < pauseEndTime){
                 yield return 0;
             }
             break;
         }
     }
 }

Usabilty:

 yield return StartCoroutine(CoroutineUtilities.WaitForRealTime(1));

http://rontavstudio.com/use-coroutines-independent-timescale-unity-3d/

Comment
Add comment · Show 3 · 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 Owen-Reynolds · Apr 16, 2015 at 01:14 PM 0
Share

Try to avoid just links, as they tend to break. The trick in the link is to key off of realTimeSinceStartup, which isn't affected by timeScale. Then yield frames (as in an answer above) until RTSSU reaches the desired value.

avatar image brunopava · Apr 16, 2015 at 01:23 PM 0
Share

I added the code to the post. Thanks for the advise, I keep forgeting how anoying is to look for answers and click on broken links.

avatar image sunderplugs11 · Oct 27, 2016 at 10:22 PM 0
Share

ty for the script man

avatar image
1

Answer by Owen-Reynolds · Sep 08, 2014 at 04:58 PM

I think a really cheap, ugly hack might be to change the yield WaitForSeconds to yield return null;, and take smaller steps:

 for(var i : float = -10; i<0; i+=0.3) {
    ...
    yield return null;
 }

yield return null; should always wait for the next frame, regardless of timescale. But that messes up the time -- it used to finish in 10*0.1=1 second. Now it runs in 10 frames (1/6th to 1/3rd of a second?) So just rewrite the loop to take 30 small 1-frame steps.

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

14 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

Related Questions

Question about Coroutines in a Class Function 1 Answer

Unityscript Coroutine Not Running When Called 1 Answer

Cancelling Coroutine when Home button presseddown or returned main menu 1 Answer

What's stopping 'WaitForSeconds' from working? 1 Answer

'pausing' a game to wait for user to select object before proceeding 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