Displaying an end game message

EndGameMessage680x340To display a message when the game ends you need to add a few lines of code to the game script that you are using, in this example “ShowGameOverMessageGame”. Essentially, you add a event handler to the OnGameOver event that displays a message depending on the configuration.

EndGameMessage

Here is how the code looks:

if (showWinnerID)
 {
 manager.OnGameOver += OnGameOver;
 }
 else
 {
 if (customText == string.Empty)
 {
 customText = "Game Over!\nPress Space to restart";
 }
 manager.OnGameOver += () => ShowMessage(customText);
 }

The field manager.OnGameOver is an event that fires when the game ends.

The two methods used are these:

private void OnGameOver()
 {
 var winner = manager.Winners.First();
 ShowMessage(
 string.Format(
 "The winner is {0}. \nPress  to play again.", winner.PlayerID));
 }
 private void ShowMessage(string message)
 {
 text.gameObject.SetActive(true);
 text.text = message;
 }

The rules of this game are the same as the Victory Conditions examples.

To check out this example:

  • Import Abstract Strategy in a new project.
  • Import the package below into the same project.
  • Open the ShowMessageGameOver scene.

ShowMessageGameOver.unitypackage

Scroll to Top