My Unity project for some reason keeps crashing because I am trying to attach a c# script to a game object. The script is attached. None of the other scripts that I attach to game objects make Unity crash though… just this one.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DisplayTheGame : MonoBehaviour {
// the number of columns in the card grid
private int cols = 4;
// the number of rows in the card grid
private int rows = 4;
private int totalCards = 16;
// If there are 16 cards, the player needs to find 8 matches to clear the board
private int matchesNeededToWin;
// At the outset, the player has not made any matches
private int matchesMade = 0;
//Card List
private List<string>cardList = new List<string>();
// This 2d array will keep track of the shuffled,dealt cards
public Card[,]aGrid = new Card[4,4];
// This generic array list will store the two cards that the player flips over
public List<Card>aCardsFlipped;
//ACards
public List<Card>aCards;
// We'll use this flag to prevent the player from clicking buttons when we don't want him to
private bool playerCanClick;
// Store whether or not the player has won. This should probably start out false :)
private bool playerHasWon = false;
//this stores the score combo
private int combo;
//instance for the clock
public ClockScriptTest clock;
//instance for the points
public PointScriptTest point;
//arrow texture
public Texture2D arrow;
//should the arrow be shown
private bool showTutorial = true;
//should the other match message be shown
private bool displayOtherMatch = false;
//should the match as many message be shown
private bool displayMatchAsMany = false;
//should the find other match message be shown
private int otherMatchNum;
//GameOverTransition
public GameOverTransition gameOverTransition;
//Card class Object
private Card cardObj = new Card();
//Screen dimensions
private int screenWidth;
private int screenHeight;
private int sixthOfScreenW;
private int sixthOfScreenH;
private int arrowX;
private bool displayed;
//Tutorials
public Texture2D pressThisTexture;
public Texture2D findOtherMatchTexture;
public Texture2D matchAsMany;
private int howManyStars;
private bool firstBool;
private bool secondBool;
private bool thirdBool;
public Texture2D homeTexture;
public GUIStyle style;
public CardList cardListObj;
void Start (){
matchesNeededToWin = totalCards/2;
firstBool = true;
secondBool = true;
thirdBool = true;
howManyStars = 0;
displayed = true;
arrowX = (Screen.width/7) / 2;
//Screen Dimensions
screenWidth = Screen.width;
screenHeight = Screen.height;
sixthOfScreenW = screenWidth / 6;
sixthOfScreenH = screenHeight / 6;
cardList = cardListObj.LevelOneCardList();
//initializes the clock
clock.Clock();
//initializes the points
point.Points();
// We should let the player play, don't you think?
playerCanClick = true;
// Initialize some empty Collections:
// This List will store the two cards the player flips over.
aCardsFlipped = new List<Card>();
BuildDeck();
// Loop through the total number of rows in our aGrid List:
for(int i = 0; i<rows; i++)
{
// For each individual grid row, loop through the total number of columns in the grid:
for(int j = 0; j<cols; j++)
{
int someNum = Random.Range(0,aCards.Count);
aGrid[i,j] = aCards[someNum];
aCards.RemoveAt(someNum);
}
}
}
void OnGUI (){
if(displayed){
GUI.depth = 1;
//Home Button
if(GUI.Button( new Rect(0,0,sixthOfScreenW * 0.5f, sixthOfScreenH * 0.5f),homeTexture, style)){
Application.LoadLevel("Title Screen");
}
//This displays the "Find other match!" message
if(displayOtherMatch){
GUI.DrawTexture( new Rect(0, screenHeight - Screen.height/10, Screen.width, Screen.height/10), findOtherMatchTexture);
}
//This displays the "Match as many as you can before time runs out!" message
if(displayMatchAsMany){
GUI.DrawTexture( new Rect(0, screenHeight - Screen.height/10, Screen.width, Screen.height/10), matchAsMany);
}
//This displays the arrow and "Press"
if(showTutorial){
GUI.DrawTexture( new Rect(sixthOfScreenW * 0.20f,sixthOfScreenH,sixthOfScreenW * 0.7f,sixthOfScreenH * 0.5f),arrow);
GUI.DrawTexture( new Rect(0, screenHeight - Screen.height/10, Screen.width, Screen.height/10), pressThisTexture);
}
GUILayout.BeginArea ( new Rect(0,0,screenWidth,screenHeight));
BuildGrid();
if(playerHasWon)BuildWinPrompt();
if(clock.timeIsUp)BuildLosePrompt();
GUILayout.EndArea();
}
}
//Builds the game over screen
IEnumerator BuildWinPrompt (){
if (point.totalPoints >= 100) {
if (firstBool) {
howManyStars++;
firstBool = false;
}
}
if (clock.timeRemaining >= 30.0f) {
if (secondBool) {
howManyStars++;
secondBool = false;
}
}
if (point.totalPoints >= 300) {
if (thirdBool) {
howManyStars++;
thirdBool = false;
}
}
clock.clockIsPaused = true;
yield return new WaitForSeconds(1);
gameOverTransition.CreateGameOverTransition ("Level One", "You Won!", point.totalPoints, true, howManyStars);
displayed = false;
}
//Builds the lose game over screen
IEnumerator BuildLosePrompt (){
clock.clockIsPaused = true;
yield return new WaitForSeconds(1);
gameOverTransition.CreateGameOverTransition ("Level One", "You Lost!", point.totalPoints, true, 0);
displayed = false;
}
//Builds the grid of cards
void BuildGrid (){
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();
for(int i=0; i<rows; i++)
{
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
for(int j=0; j<cols; j++)
{
Card card = aGrid[i,j];
string img;
if(card.isMatched)
{
img = "blank";
} else {
if(card.isFaceUp)
{
img = card.img;
}else{
img = "faceup";
}
}
GUI.enabled = !card.isMatched;
if(GUILayout.Button((Texture2D)Resources.Load(img), GUILayout.Width(sixthOfScreenW), GUILayout.Height(sixthOfScreenH)))
{
if(playerCanClick){
FlipCardFaceUp(card);
showTutorial = false;
if(otherMatchNum == 0){
ShowBanner("Find the Other Match!");
}
}
}
GUI.enabled = true;
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
GUILayout.FlexibleSpace();
GUILayout.EndVertical();
}
//Decides which banner to show
void ShowBanner ( string banner ){
if(banner == "Find the Other Match!"){
displayOtherMatch = true;
}else if(banner == "Match as Many As You Can!"){
displayMatchAsMany = true;
}
}
//public CardClass card;
public void BuildDeck (){
// this stores a reference to a card
Card card;
int id = 0;
List<int> indexes = new List<int>();
for(int a=0; a < cardList.Count; a++){
indexes.Add(a);
}
for(int i=0; i<4; i++){
for(int j=0; j<2; j++)
{
int num = indexes.Count - 1;
string theMissingPart = cardList[num];
cardList.RemoveAt(num);
card = cardObj.CreateCard("color" + "Missing" + theMissingPart, id);
aCards.Add(card);
card = cardObj.CreateCard("color" + theMissingPart, id);
aCards.Add(card);
indexes.RemoveAt(num);
id++;
}
}
}
//This sees if the 2 cards facing up match
IEnumerator FlipCardFaceUp ( Card card ){
card.isFaceUp = true;
if(aCardsFlipped.IndexOf(card)<0){
aCardsFlipped.Add(card);
if(aCardsFlipped.Count == 2){
playerCanClick = false;
yield return new WaitForSeconds(0.4f);
if(aCardsFlipped[0].id == aCardsFlipped[1].id)
{
// Match!
aCardsFlipped[0].isMatched = true;
aCardsFlipped[1].isMatched = true;
combo++;
if(combo > 1){
point.ComboPoints(combo);
}else{
point.AddTwoPoints();
}
matchesMade++;
if(matchesMade == 1){
displayOtherMatch = false;
otherMatchNum++;
ShowBanner("Match as Many As You Can!");
}else if(matchesMade == 2){
displayMatchAsMany = false;
}
if(matchesMade >= matchesNeededToWin)
{
playerHasWon = true;
}
}else{
combo = 0;
aCardsFlipped[0].isFaceUp = false;
aCardsFlipped[1].isFaceUp = false;
}
aCardsFlipped = new List<Card>();
playerCanClick = true;
}
}
}
public class Card : System.Object{
public bool isFaceUp = false;
public bool isMatched = false;
public string img;
public int id;
Card card = new Card();
public Card CreateCard ( string img , int id ){
card.img = img;
card.id = id;
Debug.Log("card = " + card);
return card;
}
}
}