- Home /
Coroutine couldn't be started becuase inactive
I have an enemy character that uses a coroutine to cycle through idle and walking states. He is part of an object pool via the PoolManager plugin. When the character dies and is set to inactive I get the error:
"Coroutine couldn't be started because the the game object 'buffaloMale(Clone)002' is inactive!"
Is there a way to stop the coroutine when I deactivate it?
//animation cycle starts when set to active
void Start()
{
target = transform.position;
if(this.gameObject.activeInHierarchy)
{
StartCoroutine("MarchForward");
}
else
{
return;
}
}
//walking coroutine
IEnumerator MarchForward()
{
while(this.gameObject.activeInHierarchy)
{
target = transform.position + new Vector3 (0, 0, -1);
while(Vector3.Distance(transform.position, target) > 0.1f && gameObject.activeInHierarchy == true)
{
anim.SetBool ("Walking", true);
transform.position = Vector3.Lerp (transform.position, target, marchSpeed * Time.deltaTime);
yield return null;
}
transform.position = target;
anim.SetBool ("Walking", false);
yield return new WaitForSeconds (marchRate);
}
}
//deactivation method in other script
void CheckHealth()
{
if(health <= 0)
{
StopAllCoroutines();
PoolManager.Pools [poolName].Despawn (prefab.transform);
}
else
{
return;
}
}
I figured it out. I forgot that I had another script running a coroutine which started every time the enemy was hit. So I set that to only happen if(this.gameobject.activeInHeirchy)
Your answer
Follow this Question
Related Questions
How to activate and deactivate a gameObject 1 Answer
problem with enable/disable on health script 1 Answer
Activate Game object when in Camera view and deactivate when out of camera view. 2 Answers
Cannot deactivate / activate gameobject, even in editor! 6 Answers
Why deactivating objects is so slow? 1 Answer