im having a problem with my knockback feature with StartCoroutine timer, it does the work but this error always show up, anyone know how to fix this?
object of type ‘Rigidbody2D’ has been destroyed but you are still trying to access it.
heres the code
//! Attack
Collider2D[] _enemiestohurt = Physics2D.OverlapCircleAll(attackpos.position, _Attacksize, _Whatenemy);
for (int i = 0; i < _enemiestohurt.Length; i++)
{
Rigidbody2D EnemyRb = _enemiestohurt*.GetComponent<Rigidbody2D>();*
_Knockback(EnemyRb);
StartCoroutine(KnockbackTimer(EnemyRb));
_enemiestohurt*.GetComponent().Takedamage(Damage);
_}*
_TimeBtwAttk = _StartTimeBtwAttk;
//!Knockback
private void _Knockback(Rigidbody2D _AenemyRb)
{
Vector2 _KbDifference = _AenemyRb.transform.position - transform.position;
_KbDifference = _KbDifference.normalized * _KnockbackForce;
//_AenemyRb.velocity = new Vector2(_KbDifference.x * _KnockbackForce, _KbDifference.y * _KnockbackForce);
_AenemyRb.AddForce(_KbDifference, ForceMode2D.Impulse);
}
private IEnumerator KnockbackTimer (Rigidbody2D _AenemyRbTimer)
{
if(_AenemyRbTimer != null)
{
yield return new WaitForSeconds(_KnockbackTimer);
_AenemyRbTimer.velocity = Vector2.zero;
}
}
//!Enemy
public void Takedamage(int _Damage)
{
Health -= _Damage;
if (Health <= 0)
{
Death();
}
Debug.Log(“aghhh” + Health);
}