Unity Crash because of script! Please HELP!!!!

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;
	}
}
}

wrong forum section, no error log paste and hundreds of lines of code

What section should it be in then?

scripting :slight_smile:

Can someone move it then? Ostwind do you have any ideas on the issue though?

ask a mod to move it with the report button

probably no one can or will try to help before you tell what the error is

The problem is that when I try to attach the script to a game object then Unity crashes. It is just with this script though. So I wanted to know if there was something wrong with the script.

Than debug it. If it was me, I’d delete half the functions.

It work? Start adding functions back. Obviously problem is in one of them.
It didn’t? Remove more functions. Obviously problem is in one of the remaining.

I tried that Windexglow2 and I found what was causing the problem. It was this code.

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;

    }
}

I do not know what is wrong with the code though. I get an error saying:
ExecutionEngineException: SIGILL
Card…ctor ()
Card…ctor ()
Card…ctor ()
Card…ctor ()
Card…ctor ()
Card…ctor ()
Card…ctor ()

Any thoughts?

Did do you construct a card instance at line 13?

I don’t think so… Can I delete line 13 and in line 19 and 21 can I replace card with the this keyword?

Yes, indeed you have to! You are creating an endless amount of cards at the moment, I doubt you have enough memory or even a cpu to handle that :slight_smile:

I have fixed it so that Unity does not crash, but I have another problem. Here is my updated Card class.

using UnityEngine;
using System.Collections;

public class Card : System.Object{
	public bool  isFaceUp = false;
	public bool  isMatched = false;
	public string img;
	public int id;

	public void CreateCard (string img ,   int id  ){
		this.img = img;
		this.id = id;
	}
}

Here is how I create the cards:
outside the start method I create the variable of cardObj.

Card cardObj = new Card();
//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 = new cardObj.CreateCard(cardObj, "color" + "Missing" + theMissingPart, id);
			Debug.Log("card.img = " + card.img);
			Debug.Log("card.id = " + card.id);
    	 	aCards.Add(card);
     
     		card = new cardObj.CreateCard(cardObj, "color" + theMissingPart, id);
     		aCards.Add(card);
		
     		indexes.RemoveAt(num);

     		id++;
 		}
    }
}

In lines 23 and 28 I get an error saying: DisplayTheGame.cardObj’ is a field' but a type’ was expected

anybody?

CreateCard is expecting a string and an int as parameters

public void CreateCard (string img ,   int id  ){

But you’re providing it with a Card, string, and int

card = new cardObj.CreateCard(cardObj, "color" + theMissingPart, id);

You wrote way too much code with way too many inter-dependencies at once, and are in way over your head.

Start small. Just get your Card class working with no actual game mechanics, UI, or anything else. Make sure you can create cards and they do what they’re supposed to do using a simple MonoBehaviour with a Start() method that tests the Card class and does some logging.

Then add functionality one piece / function at a time.

Edit: To get you started on the right track, you should replace the CreateCard() method with a proper constructor.

That’s right. And you need to understand that if you create a card, it is not necessary to pass it a card reference!