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 /
This question was closed May 09, 2019 at 11:01 AM by adovehv for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by adovehv · May 07, 2019 at 10:56 AM · coroutineplayerprefsenemy aicoroutine errors

Stop enemy attack coroutine when player is death.

Hi! i have a coroutine for the enemy attack, i want to stop this coroutine when the player is dead, but i have this error. I stopped other coroutines same this form, but this not work. Thanks :)

alt text

alt text

stop.jpg (34.6 kB)
corutine.jpg (38.3 kB)
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

  • Sort: 
avatar image
4

Answer by YasinJavaid_ · May 07, 2019 at 11:08 AM

add this line of code instead of.

 private IEnumerator coroutine;
 
 //where you strat your couritune
 
 coroutine = Attack(time);
 StartCoroutine(coroutine);
 
 //call this to stop routine 
 
 StopCoroutine(coroutine);
Comment
Add comment · Show 7 · 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 adovehv · May 07, 2019 at 11:19 AM 0
Share

mm i'm a bit noob program$$anonymous$$g, I think I did not write it in the right place.

alt text

alt text

1.jpg (53.4 kB)
2.jpg (35.4 kB)
avatar image Bonfire-Boy adovehv · May 07, 2019 at 11:26 AM 0
Share

First, you haven't declared the coroutine variable.

Second, your StopCoroutine call needs to stop the coroutine that you started earlier, so needs a reference to the coroutine variable.

Try this...

 // to start the CR
 coroutine = StartCoroutine(Attack());

 // to stop it
 StopCoroutine(coroutine);
 

Third, please stop posting screenshots of your code. You should be using the 101010 button to post code. This will make it much easier for people to help you (because they'll be able to copy/edit/paste your code to show you how to change it.

avatar image adovehv Bonfire-Boy · May 07, 2019 at 11:41 AM 0
Share

I'm so sorry, I'm noob in to the forums. i will use the 101010 button.

avatar image YasinJavaid_ · May 07, 2019 at 11:23 AM 0
Share

post your script i will update its just simple thing

avatar image adovehv YasinJavaid_ · May 07, 2019 at 11:40 AM 0
Share
 ENE$$anonymous$$Y SCRIPT
 
 void CheckDistance()
     {
         if (Vector3.Distance(target.position,
             transform.position) <= chaseRadius
             && Vector3.Distance(target.position, transform.position) > attackRadius)
         {
             if (currentState == EnemyState.idle || currentState == EnemyState.walk
                 && currentState != EnemyState.stagger)
             {
                 ChangeState(EnemyState.attack);
                 anim.SetTrigger("attack");
                 print("rana ataca");
                 if (!attacking)
                 {
                     StartCoroutine(Attack(waitToAttack));
                 }           
             }
         }
 
 HEALTH SCRIPT
 
 if (health <= 0)
         {
             rb2d.constraints = RigidbodyConstraints2D.FreezePosition;
             print("player muerto");
             anim.SetBool("Death", true);
             swordDamage.Instance.boxColliderSword.enabled = false;
             playerColl.enabled = false;
             gameObject.tag = "Untagged";
             StopCoroutine(frog.Instance.coroutine());
         }
avatar image YasinJavaid_ · May 07, 2019 at 11:56 AM 0
Share
  public IEnumerator coroutine;
 void CheckDistance()
      {
          if (Vector3.Distance(target.position,
              transform.position) <= chaseRadius
              && Vector3.Distance(target.position, transform.position) > attackRadius)
          {
              if (currentState == EnemyState.idle || currentState == EnemyState.walk
                  && currentState != EnemyState.stagger)
              {
                  ChangeState(EnemyState.attack);
                  anim.SetTrigger("attack");
                  print("rana ataca");
                  if (!attacking)
                  {
                      coroutine = StartCoroutine(Attack(waitToAttack));
                  }           
              }
          }
          
          
          
          
          
          HEALTH SCRIPT
  
  if (health <= 0)
          {
              rb2d.constraints = RigidbodyConstraints2D.FreezePosition;
              print("player muerto");
              anim.SetBool("Death", true);
              swordDamage.Instance.boxColliderSword.enabled = false;
              playerColl.enabled = false;
              gameObject.tag = "Untagged";
              StopCoroutine(frog.Instance.coroutine());
          }
avatar image adovehv YasinJavaid_ · May 07, 2019 at 12:05 PM 0
Share

I copy your code but i have this error:

Cannot implicitly convert type UnityEngine.Coroutine' to System.Collections.IEnumerator'

avatar image
0

Answer by MichaI · May 07, 2019 at 12:39 PM

Try changing coroutine variable type to Coroutine instead of IEnumerator, like this:

       public Coroutine coroutine;
      void CheckDistance()
           {
               if (Vector3.Distance(target.position,
                   transform.position) <= chaseRadius
                   && Vector3.Distance(target.position, transform.position) > attackRadius)
               {
                   if (currentState == EnemyState.idle || currentState == EnemyState.walk
                       && currentState != EnemyState.stagger)
                   {
                       ChangeState(EnemyState.attack);
                       anim.SetTrigger("attack");
                       print("rana ataca");
                       if (!attacking)
                       {
                           coroutine = StartCoroutine(Attack(waitToAttack));
                       }           
                   }
               }
               
               
               
               
               
               HEALTH SCRIPT
       
       if (health <= 0)
               {
                   rb2d.constraints = RigidbodyConstraints2D.FreezePosition;
                   print("player muerto");
                   anim.SetBool("Death", true);
                   swordDamage.Instance.boxColliderSword.enabled = false;
                   playerColl.enabled = false;
                   gameObject.tag = "Untagged";
                   frog.Instance.StopCoroutine(frog.Instance.coroutine);
               }

Comment
Add comment · Show 3 · 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 adovehv · May 07, 2019 at 12:47 PM 0
Share

This work fine, the enemy stop attack when the player is dead, but when the player is dead, this error appears. "Coroutine continue failure"

avatar image MichaI adovehv · May 07, 2019 at 12:56 PM 0
Share

I've corrected last line, StopCoroutine should be called from object that have started it, so it would be "frog.Instance.StopCoroutine(frog.Instance.coroutine);"

avatar image adovehv MichaI · May 07, 2019 at 01:03 PM 0
Share

Ops! Sorry, it's true, thank so much, all work very good. :)

Follow this Question

Answers Answers and Comments

119 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy attack constantly with a coroutine 1 Answer

Coroutine Errors 0 Answers

Why does this co-routine not function properly after re-entering the scene? 0 Answers

Coroutine Local Scale Stops Milliseconds After Starting 1 Answer

Coroutine problems PLEASE HELP ME 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