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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by TheFattestNinja · Jan 26, 2015 at 12:30 PM · waitforsecondstimescalestartupcountdown

WaitForSeconds not (always) working on level startup

Hi, biiiiig noob here :)

I am trying to have a 3-2-1-GO! countdown on level startup. During the countdown, the time should be frozen.

This is what I have so far, and it's almost working:

     void Start () {
         countDownText.enabled = true;
         offset = transform.position;
         startCountdown ();
     }
     
     void LateUpdate () {
         transform.position = player.transform.position + offset;
     }
 
     void startCountdown() {
         StartCoroutine (getReady ());
     }
 
 
     IEnumerator getReady ()    
     {
         float epsilon = 0.000001f;
         Time.timeScale = epsilon;
 
         countDownText.text = "3";   
         yield return new WaitForSeconds(1 * Time.timeScale);
 
         countDownText.text = "2";    
         yield return new WaitForSeconds(1 * Time.timeScale);
     
         countDownText.text = "1";
         yield return new WaitForSeconds(1 * Time.timeScale);
 
         countDownText.text = "GO!";
         yield return new WaitForSeconds(1 * Time.timeScale);
         Time.timeScale = 1.0f;
 
         countDownText.enabled = false;
         TimerUpdater other = (TimerUpdater) timerText.GetComponent(typeof(TimerUpdater));
         other.ResetTimer ();
     }

It's almost working in the sense that I have two scenes, a menu one and the game one (the game one contains the code above, the menu one is literally just OnAnyKeyPressed, Application.Load the other).

The game scene has a victory condition that brings you back to the menu one when met.

What happens now is that for the first 2-3 loops of menu, win, menu, win, everything works as intended. After a while, things start to go bananas, and in the moment the main game scene is loaded the countdown goes 3-2-1-go super fast (so fast you don't even see it without debug timestamps) and the game starts immediately.

Any hints?

Thanks,

D.

Comment
Add comment · Show 6
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 TheFattestNinja · Jan 26, 2015 at 07:36 AM 0
Share

For some reason, changing the wait-one-seconds parts to:

 float epsilon = 0.0001f;
 Time.timeScale = epsilon;
 
 countDownText.text = "3";
 float nextTime = Time.time + epsilon;
 while (Time.time < nextTime) {
     yield return null;
 }

Seems to fix it. I'm O$$anonymous$$ with it, but still baffled and I'd like to have some answer about the why :/

avatar image Owen-Reynolds · Jan 26, 2015 at 05:05 PM 0
Share

Hmmm, you wait for a millionth of a second, with time slowed to one millionth speed, so they cancel to be 1 second. Some other script probably reset timeScale to 1, causing the speedup.

I'd guess the second version works because you accidentally fixed the real problem elsewhere.

timeScale=0 is a poormans's pause. Once you have to start coding exceptions, seems easier to just use a pause variable for code, and freeze rigidbodies.

avatar image TheFattestNinja · Jan 26, 2015 at 07:18 PM 0
Share

Hi Owen, thanks for your comment. I suspected that too but my game is very small (it's just an enhanced version of the roll-a-ball tutorial) and I'm sure there is no other place I alter the timeScale. From my noob point of view, I find the timeScale reduction far more elegant than putting a pause flag in every script, care to share why that approach is better?

avatar image smoggach · Jan 26, 2015 at 07:29 PM 0
Share

The other place you alter the timeScale may be the same place. $$anonymous$$ake sure you don't have the coroutine running twice and use a string ins$$anonymous$$d:

StopCoroutine("getReady"); StartCoroutine ("getReady");

Owen's strategy is better because it's more extensible, allows the program to continue doing other things at regular speed while paused, and avoids any expensive or inaccurate floating point calculations.

If this isn't the case then the actual problem is probably Unity adding it's own millionth of a second to your float somewhere along the way. (it happens to me all the time and i dont know why. 5f + 5f will usually end up as 10.0000001f or 9.99999999f)

avatar image TheFattestNinja · Jan 26, 2015 at 07:56 PM 0
Share

Should I call the StopCoroutine as the last line of the getReady function or where?

BTW, you should never test for equality using floats or double, due to their nature. Do something like if ($$anonymous$$ath.abs(v1 - v2) < epsilon) then v1 == v2 . I'm a Unity noob but I know a couple of program$$anonymous$$g tricks :)

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Time.timeScale problem 1 Answer

how to stop object before GO writes 1 Answer

Press Key for higher TimeScale and WaitForSeconds for lower TimeScale 0 Answers

run level for 4 minutes help 1 Answer

Timescale and Timers - what am I doing wrong? 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