How can I make a generalized attack function (Python Help)


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
    if nearestEnemy:
        while > 0:

this function’s working except for this line:

imagine a soldier was used in the function:


Hope this helps.
:lion: :lion: :lion: