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 DJJD · Oct 17, 2017 at 03:40 AM · waitforsecondscoroutinesstartcoroutineyield waitforsecondsstopcoroutine

Can't Stop couroutines

Hi there, I'm trying to make a game where when you are near a FireCamp, you gain HP, and else, you lose HP (like a survival game).

My code was working just fine (like a week ago), and I modified a couple of stuffs, not really in link with the HP, and the moment I put a UI with a HP Bar, I've realised that the moment I happen to be near a FireCamp, I'm losing HP and winning HP at the same time, and when I go out of the healing zone, I still win HP (which was not the case 1 week ago). I've deleted everything and wrote back everything and I still have the problem. I can't find a proper way to debug it either. Here is my code :

 void Update () {

     if (isInZone)
     {
         _startWinningLife = true;
         _startLosingLife = false;
     }
     else if (!isInZone)
     {
         _startLosingLife = true;
         _startWinningLife = false;
     }
     LifeManagement();
 }

 // Manages all the states of Life Alterations 
 void LifeManagement()
 {
     if (_startWinningLife)
     {
         if (!_isWinningLife)
         {
             WinLife();
         }
         _startWinningLife = false;
     }
     if (_startLosingLife)
     {
         if (!_isLosingLife)
         {
             LoseLife();
         }
         _startLosingLife = false;
     } 
 // Starts the losing hp cycle and stops the winning hp cycle
 void LoseLife()
 {
     _isWinningLife = false;
     StopCoroutine(LifeWinning());
     _isLosingLife = true;
     StartCoroutine(LifeLosing());
 }


 // Player loses Hp every Seconds
 IEnumerator LifeLosing()
 {
     yield return new WaitForSeconds(1);
     _hp = _hp - _overtimeDamage;
     yield return StartCoroutine(LifeLosing());
 }

 // Player wins Hp every Seconds
 IEnumerator LifeWinning()
 {
     yield return new WaitForSeconds(1);
     _hp = _hp + _overtimeHeal;
     yield return StartCoroutine(LifeWinning());
 }

Can someone please help me?

Thanks a lot

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 PizzaPie · Oct 17, 2017 at 02:55 PM 0
Share

You may also stop a couroutine by starting it with

 StastCorountine("$$anonymous$$yCoroutine$$anonymous$$ethodName");

//and stoping it with StopCoroutine("$$anonymous$$yCoroutine$$anonymous$$ethodName");

But this will stop ALL coroutines initiated with this way.

1 Reply

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

Answer by MacDx · Oct 17, 2017 at 05:09 AM

Yes, the problem here is that , the code you wrote isn't the correct way to stop a coroutine. These lines are the problem (39 and 41):

 StopCoroutine(LifeWinning());
 StartCoroutine(LifeLosing());

Coroutines are a special objects. These are called generators in C# and many other languages, but I'll stick to the point. The way you create a Coroutine (Generator) is by calling the method that returns an IEnumerator instance which is what you put inside a StartCoroutine method call. This IEnumerator method it's kind of like a constructor, so what you are doing basically is trying to stop an instance of a new coroutine, it's like if you expected this to work.

 SpawnMethod( new CoolObject());
 Destroy( new CoolObject());

Do you think the Destroy method will the destroy the same object that was passed to the Spawn method? No it won't, because they are referring to 2 completely different objects and that's what's happening in your code right now.

If you want to stop a specific instance of a coroutine you will need to save it on a field. So what you should do is add 2 fields like this:

 private IEnumerator lifeLosingCoroutine;
 private IEnumerator lifeWinningCoroutine;

Then when you start these coroutines you first save the object returned by the IEnumerator methods on to these fields and then you use StartCoroutine with these fields as the parameters, like this:

 lifeLosingCoroutine = LifeLosing();
 StartCoroutine(lifeLosingCoroutine);
 //Same thing for the life winning one

 //And then you would stop it like this
 StopCoroutine(lifeLosingCoroutine);
 lifeLosingCoroutine = null;
 //And same thing for the life winning one

Hope this helps!

Comment
Add comment · Show 4 · 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 DJJD · Oct 17, 2017 at 01:12 PM 1
Share

That helps a lot! I'm not sure if the correction is right (though I'm pretty sure it is) as I will have to correct it when I'll be home, but it looks like that does make sense.

Your explaination makes a lot of senses to me, and I appreciate it! So, just to be sure I got it right, If I would put StopCoroutine(coroutine()); It would create a coroutine, then stop it immediately?

Thanks a lot for this!

avatar image MacDx DJJD · Oct 17, 2017 at 04:11 PM 1
Share

It would create a coroutine, then stop it immediately?

Yup, which isn't very useful

avatar image DJJD MacDx · Oct 17, 2017 at 11:49 PM 0
Share

Ok I just checked out the code and I still have problems.

First, I got a null reference, because at the start of the game, both IEnumarator are set to null so it can't stop a null IEnumerator. So, I put a Null Check to be sure that's not the case when I happen to stop the coroutine. Yeah for me! It works now!

BUT! I still have the same problem as before : The moment I go near a Fire Camp, my HP starts to go up, but I still lose HP too.... I've put a Debug.Log to check if I go pass the null check to stop a coroutine, and the Log appears to work fine. But, for a reason, the Coroutine does not seems to stop? Is that possible? I've put the StopCoroutine in a String format, which should in theory stop every Coroutine with the String name, and nothing happens neither....

I'm crying inside right now. It's a custom game I had to make for a class (which I should give to the $$anonymous$$cher tomorrow night), and that is the last thing I need to do...and the first I did back when it worked....

If you know what is going on... I'd be thanksfull senpai!

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

73 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

Related Questions

Brain exploding ... Problem with an internal Coroutine manager 3 Answers

yield waitforseconds doesnt work on function start 2 Answers

how to restart coroutine 2 Answers

Is there any way to stop WaitForSeconds() ? 1 Answer

How to force Coroutine to finish 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