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 Razputin · Jun 15, 2017 at 06:09 PM · coroutinestringienumeratorstartcoroutinestopcoroutine

Coroutine won't stop running. Tried String.

My coroutine won't stop running. I've tried starting it with the string method but no luck. I've also tried declaring a coroutine and then starting and stopping the declared one but also no luck. Is it because I'm calling the coroutine from itself at the end?

     public GameObject leftAttack, rightAttack;
     public GameObject bomb;
     public int health = 5;
     public bool startBattle = false, battleStarted = false;
     public int counter = 0;
     public bool dead = false, deadCalled = false;
     private Animator animator;
     public GameObject[] bombsDropped;
 
     void Start ()
     {
         animator = GetComponent<Animator>();
     }
     
     void Update ()
     {
         if (PlayerPrefs.GetInt("Boss_Dead") == 0)
         {
             if (startBattle == true && battleStarted == false)
             {
                 Debug.Log("STARTING BATTLE");
                 StartCoroutine("BossRoutine");
                 battleStarted = true;
             }
             if (health <= 0)
             {
                 dead = true;
             }
             if(dead == true && deadCalled == false)
             {
                 Debug.Log("HI");
                 StopCoroutine("BossRoutine");
                 animator.SetInteger("CurrentState", 0);
                 StartCoroutine(Die());
             }
         }
     }
 
     void StartBattle()
     {
         startBattle = true;
     }
 
     void TakeDamage()
     {
         health = health - 1;
     }
 
     void ResetTrap()
     {
         StopCoroutine("BossRoutine");
         counter = 0;
         health = 5;
         startBattle = false;
         battleStarted = false;
         foreach(GameObject go in bombsDropped)
         {
             Destroy(go);
         }
         animator.SetInteger("CurrentState", 0);
     }
 
     private IEnumerator Die()
     {
         //make explosions
         deadCalled = true;
         GetComponentInParent<WallManager>().bossDead= true;
         yield return new WaitForSeconds(5);
         //PlayerPrefs.SetInt("Boss_Dead", 1);
         Destroy(gameObject);
     }
 
     private IEnumerator BossRoutine()
     {
         yield return new WaitForSeconds(1);
         animator.SetInteger("CurrentState", 1);
         yield return new WaitForSeconds(1);
 
         animator.SetInteger("CurrentState", 2);
         yield return new WaitForSeconds(.5f);
         Instantiate(rightAttack, new Vector3(transform.position.x, transform.position.y - 3, 0), Quaternion.identity);
         yield return new WaitForSeconds(.5f);
 
         animator.SetInteger("CurrentState", 3);
         yield return new WaitForSeconds(.5f);
         Instantiate(leftAttack, new Vector3(transform.position.x, transform.position.y - 3, 0), Quaternion.identity);
         yield return new WaitForSeconds(.5f);
 
         animator.SetInteger("CurrentState", 4);
         yield return new WaitForSeconds(1);
 
         animator.SetInteger("CurrentState", 5);
         yield return new WaitForSeconds(1);
 
         animator.SetInteger("CurrentState", 6);
         yield return new WaitForSeconds(.6f);
         Instantiate(leftAttack, new Vector3(transform.position.x, transform.position.y - 3, 0), Quaternion.identity);
         yield return new WaitForSeconds(.4f);
 
         animator.SetInteger("CurrentState", 7);
         yield return new WaitForSeconds(.6f);
         Instantiate(rightAttack, new Vector3(transform.position.x, transform.position.y - 3, 0), Quaternion.identity);
         yield return new WaitForSeconds(.4f);
 
         animator.SetInteger("CurrentState", 8);
         yield return new WaitForSeconds(1);
 
         animator.SetInteger("CurrentState", 9);
         yield return new WaitForSeconds(.2f);
         if (counter < 5)
         {
             bombsDropped[counter] = Instantiate(bomb, new Vector3(transform.position.x + Random.Range(-6, 6), transform.position.y - 3.5f, 0), Quaternion.identity) as GameObject;
             counter++;
         }
         Instantiate(leftAttack, new Vector3(transform.position.x - 1, transform.position.y - 3, 0), Quaternion.identity);
         Instantiate(rightAttack, new Vector3(transform.position.x + 1, transform.position.y - 3, 0), Quaternion.identity);
         yield return new WaitForSeconds(.8f);
 
         animator.SetInteger("CurrentState", 1);
         StartCoroutine(BossRoutine());
 
     }
 
 }
 
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

1 Reply

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

Answer by geek_freek · Jun 15, 2017 at 06:36 PM

Hello PSYCADET , I believe what you are trying to do is loop in the coroutine BossRoutine. Am I assuming this correctly ? What is the purpose of that coroutine ? If you are calling the coroutine in the end again you will not be able to stop it until you have the reference of that coroutine , but this would only mess it more. You could try looping the code inside the corouitne indefinitely. Take the reference of BossRoutine in a coroutine Object at the time of starting thia coroutine and then stop this coroutine using the same object. This might work.

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 Razputin · Jun 15, 2017 at 08:33 PM 0
Share

I ended up pulling the StartCoroutine from the bottom of the routine into update and used a bool to reset the routine when it finished. But have an accept since you were kind enough to respond.

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

69 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

Related Questions

Item duration doesn't stack 3 Answers

Is there any performance differences between Coroutine methods ? 3 Answers

Can I use StartCoroutine inside of the same coroutine with a new variable? 0 Answers

Make a function that can return string or IEnumerator 2 Answers

Coroutines IEnumerator not working as expected 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