// Update is called once per frame void Update() { if (hasEnemy && enemy != null) { //attack if it has an enemy enemyDistance = Vector3.Distance(enemy.transform.position, transform.position); if (enemyDistance <= attr.GetAttackRange()) { if (!attacking) { Invoke("ApplyDamage", attr.GetAttackSpeed()); //do damage every attackInt seconds attacking = true; } } if (enemy.GetComponent <UnitAttributes>().GetUnitHP() <= 0) { Destroy(enemy); ai.SetTarget(null); enemy = null; hasEnemy = false; } } else { //search for an enemy this.SetEnemy(FindClosestEnemy(enemyTag)); } }