Here comes the drop

With this groundwork literally laid, we're ready to add some code so that the X and O pieces fall into the Grid when the Squares are clicked.

Declare the following variables at the top of the GameLogic script, beneath the #pragma strict line:

  • var XPiece:GameObject;
  • var OPiece:GameObject;

Click on the GameLogic GameObject. Drag the X and O pieces into the Inspector in those variable slots.

Here comes the drop

Add this line to the ClickSquare function:

print("Square " + x + "," + y + " was clicked");
Instantiate(XPiece, new Vector3(-2.8,1,-19.7), Quaternion.identity); 

This line creates a new instance of the XPiece Prefab at a hard-coded position on the ...

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.