Displaying an end game message

To 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.

Here is how the code looks:

C#

1

2

3

4

5

6

7

8

9

10

11

12

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:

C#

1

2

3

4

5

6

7

8

9

10

11

12

privatevoidOnGameOver()

{

varwinner=manager.Winners.First();

ShowMessage(

string.Format(

"The winner is {0}. \nPress to play again.",winner.PlayerID));

}

privatevoidShowMessage(stringmessage)

{

text.gameObject.SetActive(true);

text.text=message;

}

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