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
6
Question by Catlinman · Aug 14, 2012 at 07:50 PM · javascriptyieldwaitforsecondstime.timetime.scale

Yield WaitForSeconds outside of time.Scale

When I call yield WaitForSeconds(Time) in my script and time.Scale is 0.0 it will never finish yielding. Is there a way to make WaitForSeconds run outside of time.Scale, say continue to make it run at time.Scale 1.0?

EDIT: What I was actualy looking for is when you press a button the game pauses (Time.Scale 0.0) and the Gui displays. I however need to use a yield statement while the gui is on and Time.Scale is 0.0 to display another GuiTexture after the yield but I can't do this because yield WaitForSeconds uses Time.Scale for the seconds. So what I am asking for is if there is a way to yield WaitForSeconds but not with the seconds using the time.scale which is 0.

EDIT[2013] I have moved on to more complex projects and I am not really working with Unity at the moment. If anyone else is still having this problem or has any questions I think they should still be able to ask in the comments. Otherwise I think that this question can be closed. I actually don't have the time to check if any of the answers work right now. Still I would like to thank you guys for reviving this question :). I'm pretty sure that I would now be able to solve it myself as I am a bit more experienced then I was when I asked the question. Still, thanks for the help.

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

7 Replies

· Add your reply
  • Sort: 
avatar image
43
Best Answer Wiki

Answer by mathiassoeholm · Dec 06, 2013 at 01:55 PM

You could make a utility method like this

 public static class CoroutineUtil
 {
     public static IEnumerator WaitForRealSeconds(float time)
     {
         float start = Time.realtimeSinceStartup;
         while (Time.realtimeSinceStartup < start + time)
         {
             yield return null;
         }
     }
 }

And then use it like this

 private IEnumerator MyCoroutine()
 {
     // Do stuff
 
     yield return StartCoroutine(CoroutineUtil.WaitForRealSeconds(DURATION));
 
     // Do other stuff
 }
Comment
Add comment · Show 8 · 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 DanjelRicci · Mar 31, 2014 at 06:35 PM 1
Share

Found this just now, works perfectly. Thank you very much!

avatar image _Adriaan · Nov 10, 2014 at 10:54 AM 0
Share

Perfect, thank you!

avatar image atomicjoe · Mar 13, 2015 at 01:31 AM 0
Share

You saved me from a headache! THAN$$anonymous$$S! :D

avatar image jorjdboss · Apr 30, 2015 at 11:05 AM 0
Share

Awesome! Works great!

avatar image NinjaISV · Mar 05, 2016 at 07:55 PM 0
Share

$$anonymous$$an! Thank you tons! I have having a rough time with this! :)

Show more comments
avatar image
26

Answer by hannesdvl · Nov 10, 2016 at 10:48 AM

WaitForSecondsRealtime was added in Unity 5.4

 IEnumerator MyCouroutine(float waitTime)
 {
     yield return new WaitForSecondsRealtime(waitTime);
     //DO STUFF
 }


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 nipunasudha · Jul 30, 2018 at 02:19 PM 0
Share

THIS IS THE UPDATED CORRECT ANSWER

avatar image SaumyaSaurav · Sep 08, 2018 at 07:22 AM 0
Share

This is What i Wanted, Works Perfectly Thanks $$anonymous$$an

avatar image
3

Answer by benni05 · Jun 27, 2013 at 10:54 AM

You can also use this trick like this:

 IEnumerator wait5sec()    {
     float start = Time.realtimeSinceStartup;
     while (Time.realtimeSinceStartup < start + 5f) {
         yield return null;
     }
     // do what you like
 }

 

Comment
Add comment · Show 1 · 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 DanjelRicci · Mar 31, 2014 at 06:37 PM 0
Share

I tried to use the while command before, but it blocks the execution of the entire game until the while condition has been reached.

avatar image
3

Answer by Alex_95 · Jun 19, 2016 at 12:42 PM

@Catlinman, you can use CustomYieldInstruction for this.

 public sealed class WaitForRealSeconds : CustomYieldInstruction
 {
     private readonly float _endTime;
 
     public override bool keepWaiting
     {
         get { return _endTime > Time.realtimeSinceStartup; }
     }
 
     public WaitForRealSeconds(float seconds)
     {
         _endTime = Time.realtimeSinceStartup + seconds;
     }
 }

Now you can use it like this:

 private IEnumerator CustomCoroutine()
 {
     //your stuff
 
     yield return new WaitForRealSeconds(1.0f);
     
     //your other stuff
 }
Comment
Add comment · Show 1 · 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 Tzelon · Jul 23, 2016 at 09:58 AM 0
Share

Very elegant solution. Thank you!

avatar image
0

Answer by mgreene1 · Feb 29, 2016 at 04:32 AM

To put this problem to bed (hopefully):

K, so here's how I fixed this. In my game, I have ranged weapons. I picked up an excellent tip from a game design tutorial; a good bit of feedback is to cause a fraction of a second of a pause to your game (say, 40 ms) when your projectiles impact enemies. Your eyes won't register the pause, but your brain will, making impacts register a little better.

But I digress.

The point is that I wanted to add a complete pause to all game play for 40 ms every time a significant bit of feedback occurs (you hit an enemy, the enemy dies, an explosion occurs, etc). I tried to use

 StartCoroutine(Function());
 
 _IEnumerator Function()
 {
 //setting timescale to 0 will crash your game, but such a lower number is basically the same thing ^_^-b
 Time.timeScale = .001f;
 yield return new WaitForSeconds(.004f);
 Time.timeScale = 1f;
 }

This does not work. It has something to do with how CoRoutines work and how they are updated not every frame or something blah blah blah. I'm not a CS engineer; I'm a game designer. Luckily, I heard from another post that the mst accurate timer in Unity (maybe not true, correct me pls) is InvokeRepeating("StringFunctionName", [time until start], [repeat rate]).

Armed with this new knowledge, I solved it like so:

  void OnCollisionEnter2D(Collision2D col) 
     {  
         string objectCollidedWith = col.collider.gameObject.tag;
         
         if (objectCollidedWith != "Player")
         {
             //If it collided with an enemy
             if (objectCollidedWith == "Enemy") 
             {
                 //lots of designer magic
 
 //calls for my desired "pause"
             ProjectileSleep();
 
 //ACCURATELY waits .004ms to undo my pause. Works like a charm! I multiply the .004ms by timeScale //or else it will take longer than you want it to. The repeat here doesn't matter because the bullet //destroys itself before it can occur.
             InvokeRepeating("WakeUp", .004f * Time.timeScale, 1F);
             
 
         }
     }
 
     void ProjectileSleep()
     {
 //starts the "pause"
         Time.timeScale = .001f;
 
 //check to make sure it's happening
         Debug.Log (Time.timeScale);
     }
 
     void WakeUp()
     {
 //return timeScale to normal
         Time.timeScale = 1f;
 
 // And destroy the bullet, ez peasy
         DestroySelf();
     }

I hope this can be referenced as a possible solution to this pain in the ass that is working in real time with CoRoutines and slow motion/pausing.

Keep designing, true believers! Excelsior!

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
  • 1
  • 2
  • ›

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

21 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

Related Questions

Why Is yield return new WaitForSeconds() not working 2 Answers

Definition of Script.function() depends on Script.function()... 3 Answers

Script gets stuck on WaitForSeconds() 1 Answer

yield WaitForSeconds waits for too long. 2 Answers

Yield and WaitForSeconds not stopping at correct points? 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