Need-to-know basis

It's an improvement, but that line is still monstrous! Notice the repeated element square.GetComponent.<Square>().y. Why say it three separate times, when we can say it once and store it in a variable?

We'll probably need to know square.GetComponent.<Square>().x as well to check the columns. So let's just store the first part, square.GetComponent.<Square>(), in a variable as follows:

function CheckForWin(square:GameObject):boolean
{
  var theScript:Square = square.GetComponent.<Square>();

So now the code for checking the square's column becomes:

if(aGrid[0,theScript.y].GetComponent.<Square>().player == currentPlayer && aGrid[1, theScript.y].GetComponent.<Square>().player == currentPlayer && aGrid[2, theScript.y].GetComponent.<Square>().player ...

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.