Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 MichaelJT · Mar 07, 2018 at 05:22 PM · coroutinecoroutinesmemory leak

If the While Condition of a Coroutine is false is the coroutine halted?

So I have this little tidbit of code:

 [Server]
     IEnumerator Regeneration()
     {
         while (!isDead)
         {
             yield return new WaitForSeconds(1f);
             Debug.Log("Regeneration is running.");
 
             float regenhealth;
             float regenStam;
             float regenMana;
 
             if (currentHealth < maxHealth)
             {
                 regenhealth = healthRegenerationRate;
                 //Debug.Log("Player should regenerate health.");
             }
             else
                 regenhealth = 0f;
 
             if (currentStamina < maxStamina)
             {
                 regenStam = staminaRegenerationRate;
                 Debug.Log("Player should regenerate stamina.");
             }
             else
                 regenStam = 0f;
 
             if (currentMana < maxMana)
             {
                 regenMana = manaRegenerationRate;
                 Debug.Log("Player should regenerate mana.");
             }
             else regenMana = 0f;
 
             if (regenhealth > 0f || regenStam > 0f || regenMana > 0f)
             {
                 Regenerate(regenhealth, regenStam, regenMana);
             }
         }
     }


When my player dies, isDead is set to true and the coroutine stops - as intended. My question is, is that coroutine completely stopped or do I need to kill it manually?

I'm afraid that I'm actually starting multiple coroutines all in memory with new memory allocated for each one, which will eventually kill the game.

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
1
Best Answer

Answer by MacDx · Mar 07, 2018 at 05:27 PM

When it goes out of the while loop the coroutine is completely stopped. You do not need to kill it manually or do anything else.

"I'm afraid that I'm actually starting multiple coroutines all in memory with new memory allocated for each one, which will eventually kill the game".

That depends on when you're calling StartCoroutine(Regeneration()); . If you're doing it inside a Start (or basically just calling it once) then there's no problem. If you are calling it inside Update then you do have a problem.

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 MichaelJT · Mar 07, 2018 at 05:47 PM 1
Share

Thanks for the answer.

I thought that would be the case but I wanted to make sure.

I'm basically starting the coroutine when the player first spawns and when the player respawns - its stopped when the player dies.

avatar image
0

Answer by Raimi · Mar 07, 2018 at 05:29 PM

Add

 yield break;

at the end of the coroutine and that should make sure the corountine exits once done.

Comment
Add comment · Show 10 · 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 MacDx · Mar 07, 2018 at 05:30 PM 0
Share

That's completely unnecessary in this case. You use yield break when you want to stop while you are at the middle of the coroutine, not at the end when it will exit on its own anyways.

avatar image Raimi MacDx · Mar 07, 2018 at 05:34 PM 0
Share

Thats fine, but if he is worried about it finishing it wont hurt to add it. I just adding more info in case he didn't know about it. It wont add any noticeable overhead and clearly shows what he wants to happen in the code.

avatar image MacDx Raimi · Mar 07, 2018 at 05:37 PM 1
Share

Giving him extra information is fine. Adding redundant code is not the way to go though, it's a bad habit.

For instance you'd never do this

 private void Some$$anonymous$$ethod()
 {
     while(booleanFieldIsTrue)
     {
         //Foo work
     }
     return; //Redundant
 }

Would you?

Show more comments

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

80 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

Related Questions

Is it possible to invoke a button Press With a Coroutine? 1 Answer

coroutine not running after yield new WaitForSeconds 1 Answer

How to properly implement a constant queue of actions in the background,How to properly implement a continuous background queue of actions? 1 Answer

coroutine help 2 Answers

problem with coroutines 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