Time for action – it takes two to Tic Tac Toe

A one-playerTic Tac Toe game sounds like a recipe for a bad time. Let's get the O's in there to liven things up.

Add this line to the GameLogic script:

var XPiece:GameObject;
var OPiece:GameObject;
var currentPlayer:int = 1;

And later:

function ClickSquare(square:GameObject)
{
  var piece:GameObject;
  if(currentPlayer == 1)
  {
    piece = XPiece;
  } else {
    piece = OPiece;
  }

  Instantiate(piece, square.transform.position, Quaternion.identity);

  currentPlayer ++;
  if(currentPlayer > 2) currentPlayer = 1;
}

Try it out. Now when you click, the game alternates between X's and O's.


Time for action – it takes two to Tic Tac Toe

What just happened – alternating ...

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.