Hi, I am new to Unity and I was wondering,
What would be the way to for the player to get Game over if all the players characters. I know how I would do it for one character but I have no idea how to do it for multiple characters, should I tag all the playable characters and check if their healt is 0 or is there a simpler way to do it. Thank in advance
Have a GameManager that subscribes to Unit’s OnAnyUnitSpawned and AnyUnitDead method.
List<Unit> playerUnits = new List<Unit>();
List<Unit> enemyUnits = new List<Unit>();
void Unit_OnAnyUnitSpawned(object Sender, Unit unit)
{
if(unit.IsEnemy()) enemyUnits.Add(unit);
else playerUnits.Add(unit);
}
void Unit_AnyUnitDead(object sender, Unit unit)
{
if(unit.IsEnemy())
{
enemyUnits.Remove(unit);
if(enemyUnits.Count==0)
{
//Victory
}
} else
{
playerUnits.Remove(unit);
if(playerUnits.Count==0)
{
//Defeat
}
}
}
The UnitManager in the course already has a list of all Friendly and Enemy units, so you can use that and the Health System to check if the friendly units are all dead.
Like Brian mentioned you can check for the event when they die to run that logic and test for game over.
I forgot we had those lists in the UnitManager (too many courses in my head sometimes, hehe). Saves us a bit of trouble maintaining a new Dictionary. In fact, the UnitManager itself could be responsible for telling the GameManager that a Victory or Defeat has occurred.
Thank you for the feedback, this helps alot. I will try my best to get it to work.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.