Miles Kim
Miles Kim

Reputation: 47

Why don't my if statements check the values of my function correctly?

I have code elsewhere that puts a pointer in a position based on the AttackInfo.Target value. I also have code that determines whether my characters are dead, using the Stats[lifeStatus] array reference, where 0 is dead and 1 is alive.

I want the Target to skip over the values of dead characters so you can only select living characters to attack. However, it seems to have no effect.

Why does it not work?

else if (state == CHARACTER1TARGETCHOICE) {
    attackButton.enabled = false;
    abilitiesButton.enabled = false;
    itemButton.enabled = false;
    defendButton.enabled = false;
    fleeButton.enabled = false;
    if (pointer.GetComponent<SpriteRenderer> ().enabled == false) {
        pointer.GetComponent<SpriteRenderer> ().enabled = true;
    }
    if (Input.GetKeyDown (KeyCode.UpArrow)) {
        character1AttackInfo.Target -= 1;
        bool checkTargetAlive = false;
        while (checkTargetAlive) {
            if (enemy1Stats [lifeStatus] == 0 && character1AttackInfo.Target == 1) {
                character1AttackInfo.Target -= 1;
            } else if (enemy2Stats [lifeStatus] == 0 && character1AttackInfo.Target == 2) {
                character1AttackInfo.Target -= 1;
            } else if (enemy3Stats [lifeStatus] == 0 && character1AttackInfo.Target == 3) {
                character1AttackInfo.Target -= 1;
            } else if (enemy4Stats [lifeStatus] == 0 && character1AttackInfo.Target == 4) {
                character1AttackInfo.Target -= 1;
            } else if (character1AttackInfo.Target == 0) {
                character1AttackInfo.Target = 4;
            } else {
                checkTargetAlive = false;
            }
        }
    }

In this part of the script, the lower the enemy's number, the higher they are on the screen. Therefore, when the up arrow is pressed, the target's number goes down so the cursor will move up. The cursor moving to the different places above the characters already works perfectly. The target just wont skip for some reason. Once an enemy dies and so enemy1Stats == 0 is true and the Target is 1 also, it still doesn't skip.

Upvotes: 1

Views: 85

Answers (2)

C. Helling
C. Helling

Reputation: 1402

That block of code isn't called:

bool checkTargetAlive = false; 

followed by

while (checkTargetAlive)

As a general comment, I don't quite understand how your game logic is supposed to work, but that block in the question has some definite code smell. That can't possibly be the cleanest way to implement your game logic.

Upvotes: 1

Miles Kim
Miles Kim

Reputation: 47

So I just accidentally set checkTargetAlive to false ahead of time. That made it so the while loop never ran, which made the cursor not work correctly. Just setting it to true ahead of time made it work like a charm:

else if (state == CHARACTER1TARGETCHOICE) {
    attackButton.enabled = false;
    abilitiesButton.enabled = false;
    itemButton.enabled = false;
    defendButton.enabled = false;
    fleeButton.enabled = false;
    if (pointer.GetComponent<SpriteRenderer> ().enabled == false) {
        pointer.GetComponent<SpriteRenderer> ().enabled = true;
    }
    if (Input.GetKeyDown (KeyCode.UpArrow)) {
        character1AttackInfo.Target -= 1;
        bool checkTargetAlive = true;
        while (checkTargetAlive) {
            if (enemy1Stats [lifeStatus] == 0 && character1AttackInfo.Target == 1) {
                character1AttackInfo.Target -= 1;
            } else if (enemy2Stats [lifeStatus] == 0 && character1AttackInfo.Target == 2) {
                character1AttackInfo.Target -= 1;
            } else if (enemy3Stats [lifeStatus] == 0 && character1AttackInfo.Target == 3) {
                character1AttackInfo.Target -= 1;
            } else if (enemy4Stats [lifeStatus] == 0 && character1AttackInfo.Target == 4) {
                character1AttackInfo.Target -= 1;
            } else if (character1AttackInfo.Target == 0) {
                character1AttackInfo.Target = 4;
            } else {
                checkTargetAlive = false;
            }
        }
    }

Upvotes: 0

Related Questions