- Home /
The question is answered, right answer was accepted
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 :)
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);
mm i'm a bit noob program$$anonymous$$g, I think I did not write it in the right place.
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.
I'm so sorry, I'm noob in to the forums. i will use the 101010 button.
post your script i will update its just simple thing
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());
}
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());
}
I copy your code but i have this error:
Cannot implicitly convert type UnityEngine.Coroutine' to
System.Collections.IEnumerator'
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);
}
This work fine, the enemy stop attack when the player is dead, but when the player is dead, this error appears. "Coroutine continue failure"
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);"
Ops! Sorry, it's true, thank so much, all work very good. :)