samedi 6 novembre 2021

Unity C# if statement fails?

I'm trying to learn Unity to try and pursue my dream job of being a game designer. I came across a weird thing that I don't understand why it doesn't seem to work.

Essentially I have a turn based battle system that otherwise works, but I'm trying to add a turn limit. What I have set up is basically:

int Clock = 5;

Clock--;

if (Clock <= 0)
{
    EndCombat();
}

It never seemed to end combat, despite successfully updating the health bars that is done right before the if statement. I tried several things such as putting 0 >= Clock instead of Clock <= 0, changing text in the HUD to see if the if statement is even triggering, I've even just set Clock = 0 right before the if statement, but it doesn't seem to trigger and I can't figure out why.

Currently it sets Clock to 5, then after any turn (player or enemy) it does Clock--; and then calls this method:

void UpdateCombatHUD()
{
    LeftHUD.SetHpAndMp(LeftUnit);
    RightHUD.SetHpAndMp(RightUnit);
    Timer.text = "Time: " + Clock;

    if (Clock <= 0)
    {
        StartCoroutine(HELLO());

        if (LeftUnit.CurrentHp > RightUnit.CurrentHp)
        {
            state = CombatState.Won;
            EndCombat();
            return;
        }
        else if (LeftUnit.CurrentHp < RightUnit.CurrentHp)
        {
            state = CombatState.Lost;
            EndCombat();
            return;
        }
        else if (LeftUnit.CurrentHp == RightUnit.CurrentHp)
        {
            state = CombatState.Draw;
            EndCombat();
            return;
        }
        else
        {
            return;
        }
    }
}

LeftHUD.SetHpAndMp makes sure the player's Hp and Mp bars are accurate.

RightHUD.SetHpAndMp makes sure the enemy's Hp and Mp bars are accurate.

StartCoroutine(HELLO()); is supposed to display a message and then wait 5 seconds before it does anything else (I used this to try and help me figure out what the problem is, but StartCoroutine(HELLO()) is never triggered).

Aucun commentaire:

Enregistrer un commentaire