Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
1
Question by AnishsUsername · Jul 07, 2018 at 04:52 PM · coroutineyieldwaitforsecondsyield waitforsecondsstopcoroutine

Is there any way to stop WaitForSeconds() ?

Hello,

I recently have an issue, where I want to stop a Coroutine, if the bool secondChanceUsed is equal to true. But I have no idea to do so, because StopCouroutine() will stop the whole coroutine and yield break will not let calling the win() function.

I hope you can help me out with this and thanks in advance!

IEnumerator countdown(){ yield return new WaitForSeconds(5f); if(!secondChanceUsed){ enableLooseUI(); disablePreLooseUI(); updateTotalPoints(); pointsystem.resetLocalscore(); } else{ // Stop waiting for 5 Seconds and continue with win(); }

Comment
Add comment · Show 1
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 dan_wipf · Jul 07, 2018 at 05:41 PM 0
Share

maybe put the waitforsecond in the if(!secondchanceused) and change else to if(secondchancedused)..

or put ins$$anonymous$$d of an number in waitforsecond a int’which you can modify in your if statements.

1 Reply

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

Answer by Hellium · Aug 01, 2018 at 07:18 AM

No, it's not possible to stop a WaitForSeconds only. Here is how to handle your problem:

 IEnumerator countdown()
 {    
     for( float timer = 5 ; timer >= 0 ; timer -= Time.deltaTime )
     {
         if( secondChanceUsed )
         {
             win();
             secondChanceUsed = false;
             yield break ;
         }
         yield return null ;
     }
     enableLooseUI();
     disablePreLooseUI();
     updateTotalPoints();
     pointsystem.resetLocalscore();
 }
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 romainbayer105 · Aug 29, 2020 at 11:44 AM 0
Share

What is the point of using IEnumerator then? Why not just doing it in a simple function?

avatar image Hellium romainbayer105 · Aug 29, 2020 at 11:49 AM 0
Share

Because a simple function can't wait. The coroutine allows you to wait until 5 seconds have elapsed OR secondChanceUsed turns true.

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

97 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 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 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 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 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

yield WaitForSeconds preventing further actions. 1 Answer

What is wrong with this use of WaitForSeconds? 1 Answer

Please help with While Loops 1 Answer

Alternative to "yield return WaitForSeconds()" in Coroutine with .NET Framework (Unity 2018.4) 0 Answers

Update() can not be a coroutine. 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