Time for action – notify the winner

Let's create that ShowWinnerPrompt function we just called:

function ShowWinnerPrompt()
{
  if(currentPlayer == 1)
  {
    prompt.text = "X gets 3 in a row. Player 1 wins!";
  } else {
    prompt.text = "O gets 3 in a row. Player 2 wins!";
  }
}

Hopefully, this code is straightforward, but since I'm contractually obligated to explain it, I will offer that this conditional statement evaluates the currentPlayer value, and shows one message or another depending on the result.

Test the game, and play until one player wins.

Time for action – notify the winner

Get Unity 4.x Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.