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
0
Question by WillGFR · Aug 31, 2013 at 03:11 PM · c#yieldwaitforsecondsstartcoroutine

C# WaitForSeconds doesn't seem to work ??

Hello everyone,

I'm trying to write a script that when the player looses, an animation is displayed (it's an explosion) then the function lose() is called. The function lose() simply waits for the animation to finished, then pause the game and tells the GUI to show the "Game Over" screen.

Here is my code :

 public void Lose()
 {
             print("Begin Lose() " + Time.time);
             StartCoroutine(Wait(3.0f));
             print("End Lose() " + Time.time);
             Time.timeScale = 0;
     guiMode = "Lose";
 }
 
 private IEnumerator Wait(float waitTime)
 {
             print("Begin wait() " + Time.time);
     yield return new  WaitForSeconds(waitTime);
             print("End wait() " + Time.time);    
 }


The log is : Begin Lose() 4.86 Begin wait() 4.86 End Lose() 4.86

And that's all !

It's as if the Wait() function is "paused", but the Lose() function goes on nonetheless and, because of the Time.timeScale = 0, the execution of the Wait() function can't be finished and everything freezes.

Why the Lose() function doesn't stop 3 seconds when the Wait() function is called ??

Thanks !

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

2 Replies

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

Answer by TrickyHandz · Aug 31, 2013 at 03:24 PM

Anything you want to be delayed has to be within the IEnumerator. So, your observation is correct and this is what happens.

  1. Lose Function starts

  2. Wait Function Begins (Delaying everything after the WaitForSeconds in the coroutine)

  3. Lose Function stops time

  4. Wait Function is halted due to the fact that time is no longer advancing

  5. Lose Function exits

To fix this, place you change to Time.timeScale within the Wait() function.

Just to reiterate starting a coroutine does not delay any aspects of the function it is called within, only behaviors within the coroutine will be delayed. Here is how the code has to be written for this to work as intended:

 public void Lose()
     {
         print("Begin Lose() " + Time.time);
         StartCoroutine(Wait(3.0f));
         // This print line will not have a
         // Delay.  It is called immediately
         // following the start of the coroutine
         print("End Lose() " + Time.time);
 
     }
 
     private IEnumerator Wait(float waitTime)
     {
         print("Begin wait() " + Time.time);
         yield return new WaitForSeconds(waitTime);
         // Change to the timescale must be called here
         // to prevent this coroutine from halting
         Time.timeScale = 0;
         guiMode = "Lose";
         print("End wait() " + Time.time);
     }
Comment
Add comment · Show 6 · 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 Jamora · Aug 31, 2013 at 03:30 PM 0
Share

Another way to fix the situation is to make Lose a coroutine as well and do yield return Wait(3.0f);

avatar image WillGFR · Aug 31, 2013 at 03:59 PM 0
Share

I tried that :

 public IEnumerator Lose()
 {
     print("Begin Lose() " + Time.time);
     yield return new  WaitForSeconds(3);
     print("End Lose() " + Time.time);
     Time.timeScale = 0;
     gui$$anonymous$$ode = "Lose";
 }


The log only prints 'Begin Lose()'. Even if I wait 1 $$anonymous$$ute, nothing happens. Why ??

avatar image TrickyHandz · Aug 31, 2013 at 04:07 PM 0
Share

@WillGFR, I have added a code example to my answer. You can't change Time.timeScale outside of the Wait() function if you want this to work as intended.

avatar image WillGFR · Sep 01, 2013 at 09:41 AM 0
Share

Thank you very much for your answer. I have a better understanding of the Yield/WaitForSeconds coroutine, and your code example works ! Thanks

avatar image TrickyHandz · Sep 01, 2013 at 01:17 PM 0
Share

I'm glad I could help.

Show more comments
avatar image
-1

Answer by citizen_rafiq · Sep 01, 2013 at 10:06 AM

void Start () { StartCoroutine(WaitForPlay(2.0f)); }

IEnumerator WaitForPlay(float waitTime) { yield return new WaitForSeconds(waitTime); print("play"); }

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

20 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

Related Questions

Another question about yield, WaitForSeconds in c# 2 Answers

Mysteries of yield 1 Answer

How can I make the health points constantly go down? 1 Answer

Only the first half of my coroutine works 1 Answer

WaitForSeconds not running 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