I am trying to create a general attack function, which can be used by both my hero and the soldiers that are summoned. Here is the my attackNearestOtherEnemy() function, which takes a parameter called unit; when I call this function, I either pass “hero” to it, or “soldier” to it. However, I get an error saying “TypeError: can’t read protected property: attack”. So, is there something wrong with my code?
def attackNearestOtherEnemy(unit): enemies = unit.findEnemies() nearestEnemy = None nearestEnemyDist = 9999 for enemy in enemies: dist = unit.distanceTo(enemy) if dist < nearestEnemyDist and enemy.type != "sand-yak": nearestEnemyDist = dist nearestEnemy = enemy pass if nearestEnemy: while nearestEnemy.health > 0: unit.attack(nearestEnemy)