Actually, the problem with your first code is that it only increments the enemyIndex when the enemy is not a yak, so when it finds a yak the loop would get stuck checking the same enemy (yak) and never go forward. You need to always increment the enemyIndex so the loop can skip the yaks: