- Home /
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());
}
}
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.
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.