Best Practices

Hi. I just thought I’d try a challenge here to set up a game over and victory conditions if all the units were killed or all the enemies were killed.

I found the enemy list a little more challenging as not all enemies are active at all times.

In LevelScripting.cs I’ve added a Get and Set if the last hider was shown. So if the last hider was deactivated and the last enemy is killed then the game goes to the next scene.

It works but I’m just asking if that would be a decent way to achieve that or can you provide any better solutions.

// LevelScripting.cs
        door2.OnDoorOpened += (object sender, EventArgs e) =>
        {   //TODO set here?
            lastSection = true;
            SetActiveGameObjectList(hider3List, false);
            SetActiveGameObjectList(enemy2List, true);
        };
    }

// same code as Hugo's for all the other functions

    public bool GetGotToTheLastSection()
    {
        return lastSection;
    }
    public void SetGotToTheLastSection(bool lastSection)
    {
        this.lastSection = lastSection;
    }

and then in UnitManager

        if (unit.IsEnemy())
        {
            enemyUnitList.Remove(unit);
            if (AreAllUnitsDead(enemyUnitList))
            {
                if (levelScripting.GetGotToTheLastSection())
                {
                    GameManager.Instance.LoadNextScene();
                    levelScripting.SetGotToTheLastSection(false);
                }
            }


        }
        else
        {
            friendlyUnitList.Remove(unit);
            if (AreAllUnitsDead(friendlyUnitList))
            {
                GameManager.Instance.LoadGameOverScene();
            }

        }

This looks like a good approach.

1 Like

Yup that looks good.

Alternatively you could just store a List on some script, like the LevelScripting, and add all enemies to that list, even the ones that start off as disabled. Then you can just check against that list of they have all been spawned+killed or not.

Alternatively you could also end the level by making the player interact with some object, like in XCOM they need to extract to finish the mission. In that interact action you could add the logic to check if all enemies have died and perhaps even allow leaving the level even if some enemies remain unspawned (like for optional areas)

1 Like

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

Privacy & Terms