Two Opposing Methods Being Called at the Same Time

Hi all! I have a question about the Glitch Garden code in our LevelController script. I just noticed that it’s possible for our IEnumerator HandleWinCondition() and public void HandleLoseCondition() methods to happen at the exact same time.

If the defenders destroy all enemies except the exact number of enemies that equals the number of lives remaining, and the remaining enemies make their way to the Damage Collider, they’ll be destroyed and call both methods because the number of enemies will be 0 and the lives text will be at 0 as well. When this happens, both the Level Complete Canvas and Level Lost Canvas overlays will appear.

I’ve been trying to disable the Level Complete Canvas in this instance, so that only the Level Lost Canvas appears, but haven’t been able to come up with the right way to do it. Any help would be appreciated! Also, I apologize if this issue has already been brought up and resolved.

If anyone stumbles across this, I came up with a solution and replicated the issue to make sure it worked.

In the LevelController script, update the AttackerKilled method to declare LivesDisplay, and access “lives” as a public float (you need to make sure lives is public) from LivesDisplay. This will make it so that in order for the win condition to be called, you have to have lives remaining. It should look like this:

public void AttackerKilled()
{
    numberOfAttackers--;
    LivesDisplay livesDisplay = FindObjectOfType<LivesDisplay>();
    if(numberOfAttackers <= 0 && levelTimerFinished && livesDisplay.lives > 0)
    {
        StartCoroutine(HandleWinCondition());
    }
}

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms