Search Unity

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

Discussion in 'Scripting' started by pokobros, Apr 6, 2014.

  1. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    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.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class DisplayTheGame : MonoBehaviour {
    6.  
    7. // the number of columns in the card grid
    8. private int cols = 4;
    9. // the number of rows in the card grid
    10. private int rows = 4;
    11. private int totalCards = 16;
    12. // If there are 16 cards, the player needs to find 8 matches to clear the board
    13. private int matchesNeededToWin;
    14. // At the outset, the player has not made any matches
    15. private int matchesMade = 0;
    16. //Card List
    17. private List<string>cardList = new List<string>();
    18. // This 2d array will keep track of the shuffled,dealt cards
    19. public Card[,]aGrid = new Card[4,4];
    20. // This generic array list will store the two cards that the player flips over
    21. public List<Card>aCardsFlipped;
    22. //ACards
    23. public List<Card>aCards;
    24. // We'll use this flag to prevent the player from clicking buttons when we don't want him to
    25. private bool  playerCanClick;
    26. // Store whether or not the player has won. This should probably start out false :)
    27. private bool  playerHasWon = false;
    28. //this stores the score combo
    29. private int combo;
    30. //instance for the clock
    31. public ClockScriptTest clock;
    32. //instance for the points
    33. public PointScriptTest point;  
    34. //arrow texture
    35. public Texture2D arrow;
    36. //should the arrow be shown
    37. private bool  showTutorial = true;
    38. //should the other match message be shown
    39. private bool  displayOtherMatch = false;
    40. //should the match as many message be shown
    41. private bool  displayMatchAsMany = false;
    42. //should the find other match message be shown
    43. private int otherMatchNum;
    44. //GameOverTransition
    45. public GameOverTransition gameOverTransition;
    46.  
    47. //Card class Object
    48. private Card cardObj = new Card();
    49.  
    50. //Screen dimensions
    51. private int screenWidth;
    52. private int screenHeight;
    53. private int sixthOfScreenW;
    54. private int sixthOfScreenH;
    55.  
    56. private int arrowX;
    57.  
    58. private bool  displayed;
    59.  
    60. //Tutorials
    61. public Texture2D pressThisTexture;
    62. public Texture2D findOtherMatchTexture;
    63. public Texture2D matchAsMany;
    64.  
    65. private int howManyStars;
    66.  
    67. private bool  firstBool;
    68. private bool  secondBool;
    69. private bool  thirdBool;
    70.  
    71. public Texture2D homeTexture;
    72. public GUIStyle style;
    73.  
    74. public CardList cardListObj;
    75.  
    76. void  Start (){
    77.     matchesNeededToWin = totalCards/2;
    78.    
    79.     firstBool = true;
    80.     secondBool = true;
    81.     thirdBool = true;
    82.    
    83.     howManyStars = 0;
    84.     displayed = true;
    85.    
    86.     arrowX = (Screen.width/7) / 2;
    87.    
    88.     //Screen Dimensions
    89.     screenWidth = Screen.width;
    90.     screenHeight = Screen.height;
    91.     sixthOfScreenW = screenWidth / 6;
    92.     sixthOfScreenH = screenHeight / 6;
    93.    
    94.     cardList = cardListObj.LevelOneCardList();
    95.    
    96.     //initializes the clock
    97.     clock.Clock();
    98.     //initializes the points
    99.     point.Points();
    100.     // We should let the player play, don't you think?
    101.     playerCanClick = true;
    102.  
    103.     // Initialize some empty Collections:
    104.     // This List will store the two cards the player flips over.
    105.     aCardsFlipped = new List<Card>();
    106.     BuildDeck();
    107.     // Loop through the total number of rows in our aGrid List:  
    108.     for(int i = 0; i<rows; i++)
    109.     {
    110.         // For each individual grid row, loop through the total number of columns in the grid:
    111.         for(int j = 0; j<cols; j++)
    112.         {
    113.             int someNum = Random.Range(0,aCards.Count);
    114.             aGrid[i,j] = aCards[someNum];
    115.             aCards.RemoveAt(someNum);
    116.         }
    117.     }
    118. }
    119.  
    120. void  OnGUI (){
    121.     if(displayed){
    122.         GUI.depth = 1;
    123.        
    124.         //Home Button
    125.         if(GUI.Button( new Rect(0,0,sixthOfScreenW * 0.5f, sixthOfScreenH * 0.5f),homeTexture, style)){
    126.             Application.LoadLevel("Title Screen");
    127.         }
    128.        
    129.         //This displays the "Find other match!" message
    130.         if(displayOtherMatch){
    131.             GUI.DrawTexture( new Rect(0, screenHeight - Screen.height/10, Screen.width, Screen.height/10), findOtherMatchTexture);
    132.         }
    133.  
    134.         //This displays the "Match as many as you can before time runs out!" message       
    135.         if(displayMatchAsMany){
    136.             GUI.DrawTexture( new Rect(0, screenHeight - Screen.height/10, Screen.width, Screen.height/10), matchAsMany);
    137.         }
    138.  
    139.         //This displays the arrow and "Press"  
    140.         if(showTutorial){
    141.             GUI.DrawTexture( new Rect(sixthOfScreenW * 0.20f,sixthOfScreenH,sixthOfScreenW * 0.7f,sixthOfScreenH * 0.5f),arrow);
    142.             GUI.DrawTexture( new Rect(0, screenHeight - Screen.height/10, Screen.width, Screen.height/10), pressThisTexture);
    143.         }
    144.    
    145.         GUILayout.BeginArea ( new Rect(0,0,screenWidth,screenHeight));
    146.         BuildGrid();
    147.         if(playerHasWon)BuildWinPrompt();
    148.         if(clock.timeIsUp)BuildLosePrompt();
    149.         GUILayout.EndArea();
    150.     }
    151. }
    152.  
    153. //Builds the game over screen  
    154. IEnumerator  BuildWinPrompt (){  
    155.     if (point.totalPoints >= 100) {
    156.         if (firstBool) {
    157.             howManyStars++;
    158.             firstBool = false;
    159.         }
    160.     }
    161.     if (clock.timeRemaining >= 30.0f) {
    162.         if (secondBool) {
    163.             howManyStars++;
    164.             secondBool = false;
    165.         }
    166.     }
    167.     if (point.totalPoints >= 300) {
    168.         if (thirdBool) {
    169.             howManyStars++;
    170.             thirdBool = false;
    171.         }
    172.     }
    173.     clock.clockIsPaused = true;
    174.     yield return new WaitForSeconds(1);
    175.     gameOverTransition.CreateGameOverTransition ("Level One", "You Won!", point.totalPoints, true, howManyStars);
    176.     displayed = false;
    177. }
    178.  
    179. //Builds the lose game over screen
    180. IEnumerator  BuildLosePrompt (){
    181.     clock.clockIsPaused = true;
    182.     yield return new WaitForSeconds(1);
    183.     gameOverTransition.CreateGameOverTransition ("Level One", "You Lost!", point.totalPoints, true, 0);
    184.     displayed = false;
    185. }
    186.                        
    187. //Builds the grid of cards  
    188. void  BuildGrid (){
    189.     GUILayout.BeginVertical();
    190.     GUILayout.FlexibleSpace();
    191.     for(int i=0; i<rows; i++)
    192.     {
    193.         GUILayout.BeginHorizontal();
    194.         GUILayout.FlexibleSpace();
    195.         for(int j=0; j<cols; j++)
    196.         {
    197.             Card card = aGrid[i,j];    
    198.             string img;
    199.            
    200.         if(card.isMatched)
    201.         {
    202.             img = "blank";
    203.         } else {
    204.             if(card.isFaceUp)
    205.             {
    206.                 img = card.img;
    207.             }else{
    208.                 img = "faceup";
    209.             }
    210.         }  
    211.        
    212.       GUI.enabled = !card.isMatched;
    213.                
    214.       if(GUILayout.Button((Texture2D)Resources.Load(img), GUILayout.Width(sixthOfScreenW), GUILayout.Height(sixthOfScreenH)))
    215.       {
    216.         if(playerCanClick){
    217.             FlipCardFaceUp(card);
    218.             showTutorial = false;
    219.             if(otherMatchNum == 0){
    220.                 ShowBanner("Find the Other Match!");
    221.             }  
    222.         }
    223.       }
    224.        
    225.     GUI.enabled = true;
    226.     }  
    227.     GUILayout.FlexibleSpace();
    228.     GUILayout.EndHorizontal();  
    229.   }
    230.   GUILayout.FlexibleSpace();
    231.   GUILayout.EndVertical();
    232. }
    233.  
    234. //Decides which banner to show
    235. void  ShowBanner ( string banner  ){
    236.     if(banner == "Find the Other Match!"){
    237.         displayOtherMatch = true;
    238.     }else if(banner == "Match as Many As You Can!"){
    239.         displayMatchAsMany = true;
    240.     }
    241. }
    242.  
    243. //public CardClass card;
    244. public void BuildDeck (){
    245.     // this stores a reference to a card
    246.     Card card;
    247.  
    248.     int id = 0;
    249.  
    250.     List<int> indexes = new List<int>();
    251.  
    252.     for(int a=0; a < cardList.Count; a++){
    253.         indexes.Add(a);
    254.     }
    255.  
    256.     for(int i=0; i<4; i++){
    257.         for(int j=0; j<2; j++)
    258.         {    
    259.             int num = indexes.Count - 1;
    260.        
    261.             string theMissingPart = cardList[num];
    262.  
    263.             cardList.RemoveAt(num);
    264.              
    265.             card = cardObj.CreateCard("color" + "Missing" + theMissingPart, id);
    266.             aCards.Add(card);
    267.      
    268.             card = cardObj.CreateCard("color" + theMissingPart, id);
    269.             aCards.Add(card);
    270.        
    271.             indexes.RemoveAt(num);
    272.  
    273.             id++;
    274.         }
    275.     }
    276. }
    277.  
    278. //This sees if the 2 cards facing up match
    279. IEnumerator  FlipCardFaceUp ( Card card  ){
    280.     card.isFaceUp = true;
    281.     if(aCardsFlipped.IndexOf(card)<0){
    282.         aCardsFlipped.Add(card);
    283.         if(aCardsFlipped.Count == 2){
    284.             playerCanClick = false;
    285.            
    286.             yield return new WaitForSeconds(0.4f);
    287.            
    288.             if(aCardsFlipped[0].id == aCardsFlipped[1].id)
    289.             {
    290.                 // Match!
    291.                 aCardsFlipped[0].isMatched = true;
    292.                 aCardsFlipped[1].isMatched = true;  
    293.                
    294.                 combo++;
    295.                
    296.                 if(combo > 1){
    297.                     point.ComboPoints(combo);
    298.                 }else{
    299.                     point.AddTwoPoints();
    300.                 }
    301.                
    302.                 matchesMade++;
    303.                 if(matchesMade == 1){
    304.                     displayOtherMatch = false;
    305.                     otherMatchNum++;
    306.                     ShowBanner("Match as Many As You Can!");   
    307.                 }else if(matchesMade == 2){
    308.                     displayMatchAsMany = false;
    309.                 }              
    310.                
    311.                 if(matchesMade >= matchesNeededToWin)
    312.                 {
    313.                     playerHasWon = true;
    314.                 }
    315.                
    316.             }else{
    317.                 combo = 0;
    318.                
    319.                 aCardsFlipped[0].isFaceUp = false;
    320.                 aCardsFlipped[1].isFaceUp = false;
    321.            
    322.             }
    323.            
    324.             aCardsFlipped = new List<Card>();
    325.          
    326.             playerCanClick = true;
    327.         }
    328.     }
    329. }
    330.  
    331. public class Card : System.Object{
    332.     public bool  isFaceUp = false;
    333.     public bool  isMatched = false;
    334.     public string img;
    335.     public int id;
    336.    
    337.     Card card = new Card();
    338.    
    339.     public Card CreateCard ( string img ,   int id  ){
    340.         card.img = img;
    341.         card.id = id;
    342.        
    343.         Debug.Log("card = " + card);
    344.        
    345.         return card;
    346.     }
    347. }
    348. }
     
  2. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,773
    wrong forum section, no error log paste and hundreds of lines of code
     
  3. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    What section should it be in then?
     
  4. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,773
    scripting :)
     
  5. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    Can someone move it then? Ostwind do you have any ideas on the issue though?
     
  6. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,773
    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
     
  7. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    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.
     
  8. Windexglow2

    Windexglow2

    Joined:
    Sep 23, 2012
    Posts:
    90
    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.
     
  9. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    I tried that Windexglow2 and I found what was causing the problem. It was this code.
    Code (csharp):
    1. public class Card : System.Object{
    2.  
    3.     public bool  isFaceUp = false;
    4.  
    5.     public bool  isMatched = false;
    6.  
    7.     public string img;
    8.  
    9.     public int id;
    10.  
    11.    
    12.  
    13.     Card card = new Card();
    14.  
    15.    
    16.  
    17.     public Card CreateCard ( string img ,   int id  ){
    18.  
    19.         card.img = img;
    20.  
    21.         card.id = id;
    22.  
    23.        
    24.  
    25.         Debug.Log("card = " + card);
    26.  
    27.        
    28.  
    29.         return card;
    30.  
    31.     }
    32. }
    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?
     
  10. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Did do you construct a card instance at line 13?
     
  11. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    I don't think so... Can I delete line 13 and in line 19 and 21 can I replace card with the this keyword?
     
  12. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    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 :)
     
  13. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    I have fixed it so that Unity does not crash, but I have another problem. Here is my updated Card class.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Card : System.Object{
    5.     public bool  isFaceUp = false;
    6.     public bool  isMatched = false;
    7.     public string img;
    8.     public int id;
    9.  
    10.     public void CreateCard (string img ,   int id  ){
    11.         this.img = img;
    12.         this.id = id;
    13.     }
    14. }
    Here is how I create the cards:
    outside the start method I create the variable of cardObj.
    Code (csharp):
    1.  
    2. Card cardObj = new Card();
    3.  
    Code (csharp):
    1.  
    2. //public CardClass card;
    3. public void BuildDeck (){
    4.     // this stores a reference to a card
    5.     Card card;
    6.  
    7.     int id = 0;
    8.  
    9.     List<int> indexes = new List<int>();
    10.  
    11.     for(int a=0; a < cardList.Count; a++){
    12.         indexes.Add(a);
    13.     }
    14.  
    15.     for(int i=0; i<4; i++){
    16.         for(int j=0; j<2; j++)
    17.         {  
    18.             int num = indexes.Count - 1;
    19.        
    20.             string theMissingPart = cardList[num];
    21.  
    22.             cardList.RemoveAt(num);
    23.              
    24.             card = new cardObj.CreateCard(cardObj, "color" + "Missing" + theMissingPart, id);
    25.             Debug.Log("card.img = " + card.img);
    26.             Debug.Log("card.id = " + card.id);
    27.             aCards.Add(card);
    28.      
    29.             card = new cardObj.CreateCard(cardObj, "color" + theMissingPart, id);
    30.             aCards.Add(card);
    31.        
    32.             indexes.RemoveAt(num);
    33.  
    34.             id++;
    35.         }
    36.     }
    37. }
    38.  
    In lines 23 and 28 I get an error saying: DisplayTheGame.cardObj' is a `field' but a `type' was expected
     
  14. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    anybody?
     
  15. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    CreateCard is expecting a string and an int as parameters
    Code (csharp):
    1.  
    2. public void CreateCard (string img ,   int id  ){
    3.  
    But you're providing it with a Card, string, and int
    Code (csharp):
    1.  
    2. card = new cardObj.CreateCard(cardObj, "color" + theMissingPart, id);
    3.  
     
  16. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    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.
     
    Last edited: Apr 7, 2014
  17. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    That's right. And you need to understand that if you create a card, it is not necessary to pass it a card reference!