Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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
0
Question by Fragmental · Oct 18, 2015 at 09:24 AM · coroutinepausewaitforsecondsyield waitforsecondstime.timescale

Is there a way to make WaitForSeconds ignore time.timescale?

Is there a way to make WaitForSeconds ignore time.timescale, or in the situation below, is there a replacement for WaitForSeconds I can use instead?

I'm using:

     while (!clicked)
     {
         yield return new WaitForSeconds(0.01f);
     }

to wait for "clicked" to be true before continuing execution of a coroutine.

I would like to be able to pause the game, but discovered that time.timescale = 0 prevents WaitForSeconds from completing so the while loop can exit.

I'm aware that I may just have to disable GameObjects/scripts/animations etc manually in order to pause them, but I wondered if there was a better way.

Thank you for reading. Any help is appreciated.

Comment
Add comment · Show 3
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 Ali-hatem · Oct 18, 2015 at 10:33 AM 0
Share

i don't know the type of your game but i faced almost the same problem i had game over background i wanted to show a seconds after the player hit enemy and i had to set time.timescale to 0 so i stop enemies been instantiated again but time.timescale stops WaitForSeconds so finally and after long time i found this idea just changed the position of the player out the screen and called the time.timescale after the game over background show up

avatar image Bunny83 Ali-hatem · Oct 18, 2015 at 10:45 AM 0
Share

If you actually need the time to go on, there's another solution. I think Eric came up with it ^^

Just set Time.timeScale to a very small number. Not to 0. That way you can simply scale all wait values as well. If you for example use 0.0001f one game second would take 10000 real seconds (~2.77 hours). For most things that's enough to make it appear "frozen". Now if you need a something to wait for 1 real second, just scale it with the same value:

 yield return new WaitForSecond(1f * 0.0001f);

That makes you wait only for a tenthousands of a second, but since the time is passing that slow it would take one real second.

Usually you only need it to go on for a few seconds. Once you're ready to actually pause your game you can set timescale to 0 to avoid if the player sits in pause for hours so he didn't actually move ^^.

avatar image Bonfire-Boy Bunny83 · Oct 14, 2016 at 06:40 PM 0
Share

Never occurred to me. One of those "obvious when you see it" things. Very clever.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Oct 18, 2015 at 10:35 AM

No, WaitForSeconds always uses Time.timeScale. However in your case you can simply use

 yield return null;

You try to wait for 0.01f seconds. Usually your framerate is lower than 100fps. So the time between two frames is already longer than the time you want to wait.

If you actually need something like WaitForSecond, you can use a coroutine like this:

 public IEnumerator _WaitForRealSeconds(float aTime)
 {
     while(aTime >0f)
     {
         aTime -= Mathf.Clamp(Time.unscaledDeltaTime,0, 0.2f);
     }
 }
 public Coroutine WaitForRealSeconds(float aTime)
 {
     return StartCoroutine(_WaitForRealSeconds(aTime));
 }

Those can be used like this in another coroutine:

 while(true)
 {
     yield return WaitForRealSeconds(2f);
     // every 2 seconds, regardless of timescale.
 }

You might want to have a look at my CoroutineHelper class. It uses a singleton to run helper coroutines indepentently from a GameObject. At the moment it doesn't contain a wait function that uses unscaledTime / unscaledDeltaTime but it could be easily added.

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 agiro · Oct 14, 2016 at 06:33 PM 0
Share

@Bunny83 When using your solution, it says the coroutine (your enumerator) has to return with a value. What should that be?

avatar image Bonfire-Boy · Oct 14, 2016 at 06:44 PM 0
Share

@Bunny83 This is more like the way I've done things more recently, but when I first started using Unity I did this kind of thing by just looking at System.DateTime.Now. I'm curious, any potential pitfalls you can think of with that? It seemed to work ok.

avatar image agiro Bonfire-Boy · Oct 14, 2016 at 06:53 PM 0
Share

For me, I put a yield return null at the end of the loop in the IEnumerator and it works just fine. I use this for UI scripting, because at character death, I scale the time down but still want to use my UI.

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

8 People are following this question.

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

Is there any way to stop WaitForSeconds() ? 1 Answer

Yield WaitForSeconds Not Working. Coroutine. 1 Answer

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

How to pause a Coroutine? 5 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