# Assigning Variables to Enemies

#1

It seems that when I assign variables to enemies, the first enemy I see gets the assignment, leaving the other ones untouchable. If I assign one enemy to enemy1, and then the other to enemy2, the first enemy will be “enemy1” and “enemy2”. If I assign more varaibels the first enemy in the line of sight will be enemy1, enemy2, enemy3…and so on.
Any way around this?

#2

Could you pls post a screenshot of what is happening? Also what level are you on?
Thanks!

#3

Try this. This makes it so you can assign a variable to the nth enemy.

``````enemies=hero.findEnemies()
enemy=enemies[0] #This finds the first enemy
enemy1=enemies[1] #This finds the second enemy
enemy2=enemies[2] #This finds the third enemy.
``````

#4

#5

you can do this instead finding each enemy 1 by 1

``````var enemy = hero.findNearestEnemy();
while (enemy){
hero.attack(enemy);
}``````

#6

I can still only target one enemy to attack. My character never picks up that there are two.

#7

Put the `var enemy=hero.findNearestEnemy();`, into a `while (true){ }` loop. It checks for an enemy every frame and attacks it.

#8

i meant to put the while enemy inside of a while true loop, sorry if i confused you

#9

Hey guys thanks for the help. This level has been solved. I ditched the loops and just told the character to do everything step by step and that worked.

#10

Put [SOLVED] on the title so everyone can see.

#11

I’m sure Enderlord meant:

``````while(true){
var enemy = hero.findNearestEnemy();
while(enemy && enemy.health > 0)
hero.attack(enemy);
}
``````

if the condition is only while(enemy) and the hero has a weak sword the result will be the message:

and the hero will freeze.