Bug: Win Condition isn't called

I just wanted to show a bug that i found in my project and how i solved it. Maybe it’s useful for someone else that has the similar problem.

So my HandleWinCondition() wasn’t called when all enemies were destroyed and the timer finished several seconds later. This was caused by the fact that the if statement that checked when to call the win condition was located in the AttackerKilled() method. So when the last attacker was killed but the timer wasn’t finished yet the win condition wasn’t called. I fixed this by also checking if the amount of attackers equals 0 in the GameTimer script when the timer was finished. This was done by creating a public method in the LevelController script to get the amount of attackers.

GameTimer:
{
if(triggerLevelFinished) { return; }

    var levelController = FindObjectOfType<LevelController>();

    GetComponent<Slider>().value = Time.timeSinceLevelLoad / levelTime;

    bool timerFinished = (Time.timeSinceLevelLoad >= levelTime);
    if(timerFinished)
    {
        levelController.LevelTimerFinished();
        if (levelController.GetAmountOfAttackers() <= 0)
        {
            StartCoroutine(levelController.HandleWinCondition());
        }
        triggerLevelFinished = true;
    }
}

LevelController:
public int GetAmountOfAttackers()
{
return amountOfAttackers;
}

1 Like

Thank you for sharing this. This was very insightful :slight_smile:

1 Like

Privacy & Terms