Hey,
I’m new to Unity and have been following “Unity 3D Game Development by Example Beginner’s Guide”. Up until today I haven’t had any problems, but this chapters code doesn’t seem to display what I want to see (It’s supposed to be a card matching game).
var cols:int = 4; // the number of columns in the card grid
var rows:int = 4; // the number of rows in the card grid
var totalCards:int = 16; // I think the answer is 16, but I was never good with numbers.
var matchesNeededToWin:int = totalCards * 0.5; // If there are 16 cards, the player needs to find 8 matches to clear the board
var matchesMade:int = 0; // At the outset, the player has not made any matches
var cardW:int = 100; // Each card's width and height is 100 pixels
var cardH:int = 100;
var aCards:Array; // We'll store all the cards we create in this Array
var aGrid:Array; // This array will keep track of the shuffled, dealt cards
var aCardsFlipped:ArrayList; // This array will store the two cards that the player flips over
var playerCanClick:boolean; // We'll use this flag to prevent the player from clicking buttons when we don't want him to
var playerHasWon:boolean = false; // Store whether or not the player has won. This should probably start out false :)
function Start()
{
playerCanClick = true; // We should let the player play, don't you think?
// Initialize the arrays as empty lists:
aCards = new Array();
aGrid = new Array();
aCardsFlipped = new ArrayList();
for(i=0; i<rows; i++)
{
aGrid[i] = new Array(); // Create a new, empty array at index i
for(j=0; j<cols; j++)
{
aGrid[i][j] = new Card();
}
}
}
function onGUI () {
GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));
BuildGrid();
GUILayout.EndArea();
print("building grid!");
}
class Card extends System.Object
{
var isFaceUp:boolean = false;
var isMatched:boolean = false;
var img:String;
function Card()
{
img = "robot";
}
}
function BuildGrid()
{
GUILayout.BeginVertical();
for(i=0; i<rows; i++)
{
GUILayout.BeginHorizontal();
for(j=0; j<cols; j++)
{
var card:Object = aGrid[i][j];
if(GUILayout.Button(Resources.Load(card.img), GUILayout.Width(cardW)))
{
Debug.Log(card.img);
}
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
What I am expecting to see is four rows and four columns with a picture of a robot on them. All I get is a white screen, no compiler errors. Is there a problem with this code? Or did I most likely just not link things up correctly? I’ve played around with this for a while but nothing seems to help. Any help would be appreciated, thanks.