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
0
Question by floralDenis · Mar 17, 2019 at 04:18 PM · coroutinewaitwhile-loop

While loop VS WaitWhile in coroutines

Hi, everyone! I`m currently working on a game, where I have lots of coroutines and sometimes I need to pause execution of some coroutines. I know two ways to solve this: 1) with while loop and "yield return null" statement 2) to use class WaitWhile (or WaitUntil) What solution is better to use? P.S. sorry for such stupid question:)

Comment
Add comment · Show 5
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 xxmariofer · Mar 18, 2019 at 08:44 AM 1
Share

i would never use a while with a yield return null, they are basically the same but it is "uglier" i would leave while loops in coroutine for other type of yield like waitforseconds, or if youo need to take into account the number of trys or something like that.

avatar image floralDenis xxmariofer · Mar 18, 2019 at 11:29 AM 0
Share

Ok, thank you very much for answer. Then I`ll use WaitUntil or WaitWhile

avatar image Bonfire-Boy xxmariofer · Mar 18, 2019 at 12:28 PM 0
Share

If your while loop is going to be empty, i.e. containing nothing but the yield, then you may as well use a yield Wait. But if you need to do something in the loop, then a yield Wait simply won't work.

avatar image xxmariofer Bonfire-Boy · Mar 18, 2019 at 12:35 PM 0
Share

thats true, but he was asking how to pause the corroutine, and thats the exact porpuse of waituntil, but obviosly if you need something to ocurr repe$$anonymous$$dly the WaitUntil wont fit. thats why i said "youo need to take into account the number of trys or something like that use the other one" but probably i didnt explain myself right, i am not native speacker.

Show more comments

3 Replies

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

Answer by Ampharel · Mar 18, 2019 at 12:43 PM

WaitUntil will hold the execution of the code until whatever value you provide is true. WaitWhile will hold the execution of the code until whatever value you provide is false.

A while loop with yield return null will hold the execution untill it breaks out of the while loop, so it's the same as the WaitWhile call.

In my opinion using the WaitUntil and WaitWhile leads to better readable code, but it's up to your preference really. There don't seem to be any real differences in execution.

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 floralDenis · Mar 18, 2019 at 12:57 PM 0
Share

Ok, thank you

avatar image
0

Answer by dargonknight · Mar 18, 2019 at 08:55 AM

I would simply create a bool, if the bool is true u do ur actions else u simply keep going normally else you can stop the coroutine save your progress and (depending on what ur doing in ur coroutine) and relaunch it after.

bool running; IEnumerator myCoroutine(float time) { if(running) { yourFunction(); } yield return new WaitForSeconds(time); StartCoroutine(myCoroutine); }

i think it's 1 of the best ways of using coroutines in your case.

Comment
Add comment · Show 5 · 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 xxmariofer · Mar 18, 2019 at 08:59 AM 0
Share

that wont work, without a loop the routine will just end. thats the same sscript as @Bluzora but missing the loop part.

avatar image dargonknight · Mar 18, 2019 at 09:04 AM 0
Share

@xxmariofer nope unlike @Bluzora my function dosn't lock you in a loop it ll simply not do your action but keep running normally until you want it to do ur action again, while a loop will force it the main difference is a loop will keep running while waitforseconds will significantly reduce the execution (complexity). you (i modified the code so it can run endlessly)

avatar image xxmariofer dargonknight · Mar 18, 2019 at 09:24 AM 1
Share

after some test for "looping routines" i ended up with this results: Update event was the fastest way of doing this, restarting over and over the corroutine gave big spikes and waituntil performance was similar than restarting the corroutine but without the spikes.

avatar image dargonknight xxmariofer · Mar 18, 2019 at 09:32 AM 0
Share

yes depending on what your doing and how frequent update might be a better option.

avatar image floralDenis · Mar 18, 2019 at 11:26 AM 0
Share

Same as with previous answer. Thank you, but that`s not what i`m asking about

avatar image
0

Answer by akashdarshan99 · Nov 13, 2020 at 11:49 AM

I'm sorry for bumping such an old thread, but i couldnt stop myself from commenting in here @dargonknight

Your method of recursively starting a coroutine is extremely bad. What you're in your case by removing the loop (supposedly to reduce the time complexity) is a grave mistake because starting a coroutine has its own overhead for calling the native code functions from te managed code, some memory allocations which means more work for the GC aswell. Instead of looping (in which you dont create any additional coroutines) when you recursively start a new coroutine, it creates unnecessary garbage, and you arent improving the time complexity because its still technically a recursion but with additional unnecessary overheads

So please don't do that, its a bad practice

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

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

Coroutine "While" Setup 1 Answer

Method returning data from WWW 1 Answer

Hold or Wait while Coroutine finishes 6 Answers

C# yield not working. 2 Answers

While loop out of control...maybe? I'm not sure. Trying to track Coroutines progress. 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