i use StopCoroutine to stop "AttackPlayer", but their keep exculte the TakeDamage() function, even i stop it.
how to totally stop the IEnumerator , use bool to turn the whileloop on and off?
void OnTriggerEnter(Collider other)
{
if (other.TryGetComponent(out Player player))
{
// Attack the player
StartCoroutine(AttackPlayer(other));
}
}
void OnTriggerExit(Collider other)
{
if (other.TryGetComponent(out Player player))
{
// Stop attacking the player
StopCoroutine("AttackPlayer");
}
}
IEnumerator AttackPlayer(Collider playerCollider)
{
Player player = playerCollider.GetComponent<Player>();
while (player != null)
{
TakeDamage();
yield return new WaitForSeconds(AttackSpeed);
}
}