i use this code , but when the health is below to 0 and destroy the gameobject , unity show the error "MissingReferenceException: The object of type 'CapsuleCollider' has been destroyed but you are still trying to access it."
void OnTriggerEnter(Collider other)
{
if (canAttack && other.CompareTag("Player"))
{
canAttack = false;
if(other != null)
{
StartCoroutine(AttackPlayer(other));
}
}
}
IEnumerator AttackPlayer(Collider playerCollider)
{
while (true)
{
playerCollider.GetComponent<Player>().TakeDamage(damageAmount);
yield return new WaitForSeconds(0.1f);
}
}
and i alreally check the colider is null before.
here is the player script
public void TakeDamage(float damage)
{
Health -= damage;
if (Health < 0)
{
Die();
}
}
private void Die()
{
Destroy(gameObject);
}
i try to check is null still no work, no debug log
IEnumerator AttackPlayer(Collider playerCollider)
{
while (true)
{
if (playerCollider.gameObject == null)
{
Debug.Log("111");
yield break;
}
playerCollider.GetComponent<Player>().TakeDamage(damageAmount);
yield return new WaitForSeconds(AttackSpeed);
}
}
```