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.
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 screen.
What just happened – to collide or not to collide?
Test the game and click on any of the invisible Squares. The X settles nicely into the top-left slot on the grid, as shown in the following screenshot. (If this doesn't happen, it's possible that you forgot to set the X or its Cube children's positions to 0
,0
,0
while you were creating the...