Enemy.health returns null? [Javascript] [Solved]


#1

Can anyone tell me why my enemy.health is returning null? I tried defining the variable within the loop and it still came back null.

// Destroy mechs and collect gold from them.

while (true) {
    var coin = hero.findNearestItem();
    // While a coin exists:
    while (coin) {
        // Move to the coin.
        hero.moveXY(coin.pos.x, coin.pos.y);
        // Reassign the coin variable to the nearest item.
        var coin = hero.findNearestItem();
    }
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        // While enemy's health is greater than 0.
     while (enemy.health > 0) {   //here enemy is null
            // Attack enemy.
            hero.attack(enemy);
            var enemy = hero.findNearestEnemy(); //oopsie
     } //oopsie
    }
}

Hey so I tried switching the level up a bit and finding enemy by an array worked?

// Destroy mechs and collect gold from them.

while (true) {
    var coin = hero.findNearestItem();
    // While a coin exists:
    while (coin) {
        // Move to the coin.
        hero.moveXY(coin.pos.x, coin.pos.y);
        // Reassign the coin variable to the nearest item.
        var coin = hero.findNearestItem();
    }
    var enemies = hero.findEnemies();
    var ei = 0;
    if (ei < enemies.length) {
        var enemy = enemies[ei];
        // While enemy's health is greater than 0.
        while (enemy.health > 0) {
            // Attack enemy.
            hero.attack(enemy);
        }
        ei ++;
    }
}

oo I ended my statement in the wrong spot lol, it’s 1 am I can’t sleep xD Probably shouldn’t be on here =P Sorry all!