Search Unity

System Null Reference Exception (card matching game)

Discussion in 'Scripting' started by Angryman100, Jul 21, 2017.

  1. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    I am making a card matching game and i have a System Null Reference Exception which only pops up when I am playing the game and not when I check for errors. I am unsure on how to fix it and would like some help.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CardControllerScript : MonoBehaviour {
    6.  
    7.     private Sprite CardBack = null;
    8.     private bool matched;
    9.     public bool check;
    10.     public bool faceUp;
    11.     private CardGame tempGame;
    12.  
    13.     void Start () {
    14.         matched = false;
    15. }
    16.         void OnMouseDown (){
    17.         if(!faceUp){
    18.            
    19.         gameObject.SetActive (false);
    20.    
    21.             CardGame.count++;
    22.             if (CardGame.CardBack != CardBack.ToString () && CardGame.count == 1){   //Null Reference Exception is for this line
    23.                 CardGame.CardBack = CardBack.ToString();
    24.             }else{
    25.                 tempGame.CheckWin(CardBack.ToString());
    26.         }
    27.     }
    28. }
    29.  
    30.     public bool GetMatched(){
    31.     return matched;
    32. }
    33.  
    34. public bool GetIsFaceUp(){
    35.     return faceUp;
    36. }
    37.  
    38. public void SeMatchedToTrue(){
    39.     matched = true;
    40. }
    41.        
    42. public void SetCardSprite(Sprite newSprite){
    43.         CardBack = newSprite;
    44.     }
    45.  
    46. }
    If someone could help me fix it that would be greatly appreciated.

    here is my other script if it helps
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CardGame : MonoBehaviour {
    6.    
    7.     public static string CardBack = null;
    8.     public static int count = 0;
    9.     private Component[] cards;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         CardBack = "";
    14.         cards = this.GetComponentsInChildren<CardGame>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.  
    20.     }
    21.  
    22.     public void CheckWin(string secondCard){
    23.  
    24.         if (count == 2){
    25.             if (CardBack == secondCard){
    26.                 Debug.Log ("Matched!");
    27.                 count = 0;
    28.                 foreach(CardControllerScript card in cards){
    29.                     if(card.ToString() == CardBack){
    30.                         Debug.Log ("you got 1 point");
    31.                         card.SeMatchedToTrue();
    32.                     }
    33.                 }
    34.             }else{
    35.                 Debug.Log ("Not match!");
    36.                 count = 0;
    37.                 foreach(CardControllerScript card in cards){
    38.                     if(card.GetMatched() == false){
    39.                         gameObject.SetActive (true);
    40.                     }
    41.                 }
    42.             }
    43.         } // if count = 2
    44.     }
    45.  
    46. }
    47.  
    48.  
     
  2. pk_Holzbaum

    pk_Holzbaum

    Joined:
    Jul 26, 2012
    Posts:
    95
    What is the exact error message (including script and line number)?
     
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I think the issue might be line 14 in CardGame
    cards = this.GetComponentsInChildren<CardGame>();
    this script is the parent gameobject, looking for all children with the same script, so if grand child will have 0 children under it and the array will be null.
    then you'll get null ref error on line 28 and line 37

    if the script CardControllerScript is in each of the children then you could change the variable cards from Component to CardControllerScript
    CardControllerScript[] cards;
    then change the get to
    cards = this.GetComponentsInChildren<CardControllerScript>();

    that should stop the null ref
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    line 22 in CardControllerScript is null because you're calling the script not the variable.
    change it to tempGame not CardGame
    1. tempGame.count++;
    2. if (tempGame.CardBack != tempGame.ToString () && tempGame.count == 1){ //Null Reference Exception is for this line
    3. tempGame.CardBack = tempGame.ToString();
    4. }else{
    5. tempGame.CheckWin(tempGame.ToString());
    6. }
    edit:
    reading this again and again
    you are creating a variable called tempGame of type CardGame, but in the If statement you where checking if CardGame.CardBack doesnot = tempGame.ToString(), So now i'm thinking that the .ToString() is an issue. you're forcing to be a string when it's not, because it's a CardGame.
    can you give me a Debug.Log(tempGame.ToString()); and tell me what is outputs
     
    Last edited: Jul 21, 2017
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    oh man and CardBack is static string that is set to null, then in Start() you set it to ""
    My second post might not work with this being a static variable.

    set it to "" from the beginning.
    change
    public static string CardBack = null;

    to
    public static string CardBack = "";
     
  6. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    I did all of your suggestions and now I have 4 errors stating that static member 'CardGame.count' cannot be accessed with an instance reference qualify with a type name instead for lines 21-23 with 2 of these for 22. all of the errors are these. I am unsure of how to fix this and I am unsure of how the Debug.log(tempGame.ToString) works and how to check the output. I am new to unity.
     
  7. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    i changed the lines below back to CardGame to fix the errors but now the System Null Reference Exception is on line 24 or the line that says }else{. How do i fix this.
    1. tempGame.count++;
    2. if (tempGame.CardBack
    3. tempGame.count
    4. tempGame.CardBack
    i also reposted the scripts.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CardControllerScript : MonoBehaviour {
    6.  
    7.     private Sprite CardBack = null;
    8.     private bool matched;
    9.     public bool check;
    10.     public bool faceUp;
    11.     private CardGame tempGame;
    12.  
    13.     void Start () {
    14.         matched = false;
    15. }
    16.         void OnMouseDown (){
    17.         if(!gameObject.SetActive (false)){
    18.  
    19.             CardGame.count++;
    20.             if (CardGame.CardBack != tempGame.ToString () && CardGame.count == 1){
    21.                 CardGame.CardBack = tempGame.ToString();
    22.             }else{ //null reference exception is this line
    23.                 tempGame.CheckWin(tempGame.ToString());
    24.             }Debug.Log(tempGame.ToString());
    25.     }
    26. }
    27.  
    28.     public bool GetMatched(){
    29.     return matched;
    30. }
    31.  
    32. public bool GetIsFaceUp(){
    33.     return faceUp;
    34. }
    35.  
    36. public void SeMatchedToTrue(){
    37.     matched = true;
    38. }
    39.      
    40. public void SetCardSprite(Sprite newSprite){
    41.         CardBack = newSprite;
    42.     }
    43.  
    44. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CardGame : MonoBehaviour {
    6.  
    7.     public static string CardBack = "";
    8.     public static int count = 0;
    9.     private Component[] cards;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         CardBack = "";
    14.         cards = this.GetComponentsInChildren<CardControllerScript>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.  
    20.     }
    21.  
    22.     public void CheckWin(string secondCard){
    23.  
    24.         if (count == 2){
    25.             if (CardBack == secondCard){
    26.                 Debug.Log ("Matched!");
    27.                 count = 0;
    28.                 foreach(CardControllerScript card in cards){
    29.                     if(card.ToString() == CardBack){
    30.                         Debug.Log ("you got 1 point");
    31.                         card.SeMatchedToTrue();
    32.                     }
    33.                 }
    34.             }else{
    35.                 Debug.Log ("Not match!");
    36.                 count = 0;
    37.                 foreach(CardControllerScript card in cards){
    38.                     if(card.GetMatched() == false){
    39.                         gameObject.SetActive (true);
    40.                     }
    41.                 }
    42.             }
    43.         } // if count = 2
    44.     }
    45.  
    46. }
    47.  
    48.  
     
  8. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    looking into this. check back in a bit. i should have an answer for you
     
  9. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    ok, so the main issue is that you have a variable tempGame but it's null, you have never assigned anything to it.
    you created a place to store it when you did "private CardGame tempGame;" but then never put anything in it.

    So you'll need to help me. what is this going to used for? you are trying to check it's CardBack against the static CardBack.
     
  10. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    i've fixed most of it, i'm getting a null ref on CardGame now.

    here is my code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CardGame : MonoBehaviour
    6. {
    7.  
    8.     public static string CardBack = "";
    9.     public static int count = 0;
    10.     private Component[] cards;
    11.     public string cardback;
    12.  
    13.     // Use this for initialization
    14.     void Start()
    15.     {
    16.         CardBack = "null";
    17.         cards = this.GetComponentsInChildren<CardControllerScript>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.  
    24.     }
    25.  
    26.     public void CheckWin(string secondCard)
    27.     {
    28.  
    29.         if (count == 2)
    30.         {
    31.             if (CardBack == secondCard)
    32.             {
    33.                 Debug.Log("Matched!");
    34.                 count = 0;
    35.                 foreach (CardControllerScript card in cards)
    36.                 {
    37.                     if (card.ToString() == CardBack)
    38.                     {
    39.                         Debug.Log("you got 1 point");
    40.                         card.SeMatchedToTrue();
    41.                     }
    42.                 }
    43.             }
    44.             else
    45.             {
    46.                 Debug.Log("Not match!");
    47.                 count = 0;
    48.                 foreach (CardControllerScript card in cards)
    49.                 {
    50.                     if (card.GetMatched() == false)
    51.                     {
    52.                         gameObject.SetActive(true);
    53.                     }
    54.                 }
    55.             }
    56.         } // if count = 2
    57.     }
    58.  
    59. }
    and

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class CardControllerScript : MonoBehaviour
    7. {
    8.  
    9.     private Sprite CardBack = null;
    10.     private bool matched;
    11.     public bool check;
    12.     public bool faceUp;
    13.     private CardGame tempGame;
    14.  
    15.     void Start()
    16.     {
    17.         tempGame = new CardGame(); //creating a new one
    18.         tempGame.cardback = gameObject.name; //seting it's local cardback name
    19.         matched = false;
    20.     }
    21.     void OnMouseDown()
    22.     {
    23.         Debug.Log("Static CardBack : " + CardGame.CardBack);
    24.         Debug.Log("tempgame.cardback : " + tempGame.cardback);
    25.         CardGame.count++;
    26.  
    27.         if (CardGame.CardBack != tempGame.cardback && CardGame.count == 1)
    28.         {
    29.             //not the same
    30.             Debug.Log("Not the same");
    31.             CardGame.CardBack = tempGame.cardback;
    32.         }
    33.         else
    34.         {
    35.             // same, so it's a match
    36.             Debug.Log("Match found");
    37.             tempGame.CheckWin(tempGame.cardback);
    38.         }
    39.  
    40.          
    41.      
    42.     }
    43.  
    44.     public bool GetMatched()
    45.     {
    46.         return matched;
    47.     }
    48.  
    49.     public bool GetIsFaceUp()
    50.     {
    51.         return faceUp;
    52.     }
    53.  
    54.     public void SeMatchedToTrue()
    55.     {
    56.         matched = true;
    57.     }
    58.  
    59.     public void SetCardSprite(Sprite newSprite)
    60.     {
    61.         CardBack = newSprite;
    62.     }
    63.  
    64. }
     
  11. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    sorry i missed this line in my script:
    Code (CSharp):
    1. tempGame = GameObject.Find ("CardGame").GetComponent<CardGame>();
     
  12. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    I changed it to your code and added the line i missed out and now the system null reference exception is for line 24 which is
    Code (CSharp):
    1. CardGame.count++;
    EDIT: just realised you already said that.
     
  13. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    i'm still working on it. give me a few more mins
     
  14. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    sure that is fine take as long as you need. Thanks for all the help by the way.
     
  15. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    All my error are removed, So I used the Card name to get matches, so like card1, card2, card3. I have 2 of each of them in my scene.
    You can remove my Debug, they are just there to help me follow the code.
    just a recap, My parent gameobject has the CardGame script attached to it. each card has the CardControllerScript attached to it.
    and each card is a subobject of the parent

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CardGame : MonoBehaviour
    6. {
    7.  
    8.     public static string CardBack = "null";
    9.     public static int count = 0;
    10.     private CardControllerScript[] cards;
    11.     public string cardback;
    12.  
    13.     // Use this for initialization
    14.     void Start()
    15.     {
    16.         CardBack = "null";
    17.         cards = this.GetComponentsInChildren<CardControllerScript>();
    18.         Debug.Log(cards.Length);
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.  
    25.     }
    26.  
    27.     public void CheckWin(string secondCard)
    28.     {
    29.         Debug.Log("CheckWin : " + CardBack + " : " + secondCard);
    30.         if (count == 2)
    31.         {
    32.             if (CardBack == secondCard)
    33.             {
    34.                 Debug.Log("Matched!");
    35.                 count = 0;
    36.                 for (int i = 0; i < cards.Length; i++)
    37.                 {
    38.                  
    39.                     if (cards[i].getCardBack() == secondCard)
    40.                     {
    41.                         Debug.Log("SeMatchedToTrue");
    42.                         cards[i].SeMatchedToTrue();
    43.                     }
    44.                  
    45.                 }
    46.                 Debug.Log("you got 1 point");
    47.             }
    48.             else
    49.             {
    50.                 Debug.Log("Not match!" + cards.Length);
    51.                 count = 0;
    52.                 for (int i = 0; i < cards.Length; i++)
    53.                 {
    54.                  
    55.                     if (cards[i].GetMatched() == false)
    56.                     {
    57.                         cards[i].gameObject.SetActive(true);
    58.                     }
    59.                     CardBack = "null";
    60.                 }
    61.             }
    62.         } // if count = 2
    63.     }
    64.  
    65. }
    66.  
    and

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CardControllerScript : MonoBehaviour
    6. {
    7.  
    8.     private Sprite CardBack = null;
    9.     private bool matched;
    10.     public bool check;
    11.     public bool faceUp;
    12.     private CardGame tempGame;
    13.  
    14.     void Start()
    15.     {
    16.         //tempGame = new CardGame(); //creating a new one
    17.         tempGame = gameObject.GetComponentInParent<CardGame>();
    18.         tempGame.cardback = gameObject.name; //seting it's local cardback name
    19.         matched = false;
    20.     }
    21.     void OnMouseDown()
    22.     {
    23.         Debug.Log("Static CardBack : " + CardGame.CardBack);
    24.         Debug.Log("cardback : " + gameObject.name);
    25.         CardGame.count++;
    26.  
    27.         Debug.Log("count = " + CardGame.count);
    28.         if (CardGame.count == 1)
    29.         {
    30.             //not the same
    31.             Debug.Log("not 2 picked");
    32.             CardGame.CardBack = gameObject.name;
    33.         }
    34.         else
    35.         {
    36.             // Check if they are the same
    37.             Debug.Log("check if same");
    38.             tempGame.CheckWin(gameObject.name);
    39.         }
    40.  
    41.          
    42.      
    43.     }
    44.  
    45.     public bool GetMatched()
    46.     {
    47.         return matched;
    48.     }
    49.  
    50.     public bool GetIsFaceUp()
    51.     {
    52.         return faceUp;
    53.     }
    54.  
    55.     public void SeMatchedToTrue()
    56.     {
    57.         matched = true;
    58.     }
    59.  
    60.     public void SetCardSprite(Sprite newSprite)
    61.     {
    62.         CardBack = newSprite;
    63.     }
    64.  
    65.     public string getCardBack()
    66.     {
    67.         return gameObject.name;
    68.     }
    69. }
     
  16. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    you still have some logic to clean up. swapping out the sprite, enabling or disabling the card when it has been matched. stopping the playing from clicking on the same card
     
  17. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    so the cards that matched have to be named the same or not
     
  18. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    yes, they need to be named the same.
    or you have to create a public string and check everything based on that string
     
  19. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    ok thanks, where you would put the enabling and disabling of the cards in the script. i would imagine that the disabling would be after the on mouse down command and the enabling would be in the not match section.
     
  20. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    so if the card was two cards named cardback that would work and two cards named cardback(1) and so on
     
  21. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    yes, that would work.

    or via a string like cardIdName
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CardControllerScript : MonoBehaviour
    6. {
    7.  
    8.     private Sprite CardBack = null;
    9.     private bool matched;
    10.     public bool check;
    11.     public bool faceUp;
    12.     private CardGame tempGame;
    13.     public string cardIdName;
    14.  
    15.     void Start()
    16.     {
    17.         if(cardIdName == "" || cardIdName == null)
    18.         {
    19.             Debug.Log("Set the name in Unity, on each card");
    20.             return;
    21.         }
    22.         tempGame = gameObject.GetComponentInParent<CardGame>();
    23.         matched = false;
    24.     }
    25.     void OnMouseDown()
    26.     {
    27.         Debug.Log("Static CardBack : " + CardGame.CardBack);
    28.         Debug.Log("cardback : " + cardIdName);
    29.         CardGame.count++;
    30.  
    31.         Debug.Log("count = " + CardGame.count);
    32.         if (CardGame.count == 1)
    33.         {
    34.             //not the same
    35.             Debug.Log("not 2 picked");
    36.             CardGame.CardBack = cardIdName;
    37.         }
    38.         else
    39.         {
    40.             // Check if they are the same
    41.             Debug.Log("check if same");
    42.             tempGame.CheckWin(cardIdName);
    43.         }
    44.  
    45.          
    46.      
    47.     }
    48.  
    49.     public bool GetMatched()
    50.     {
    51.         return matched;
    52.     }
    53.  
    54.     public bool GetIsFaceUp()
    55.     {
    56.         return faceUp;
    57.     }
    58.  
    59.     public void SeMatchedToTrue()
    60.     {
    61.         matched = true;
    62.     }
    63.  
    64.     public void SetCardSprite(Sprite newSprite)
    65.     {
    66.         CardBack = newSprite;
    67.     }
    68.  
    69.     public string getCardBack()
    70.     {
    71.         return cardIdName;
    72.     }
    73. }
     
  22. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    line 20
    line 20 which says
    Code (CSharp):
    1. return;
    is a system null reference exception
    EDIT: also how would you stop the player from selecting the same card twice if it is deactivated could they do that
     
  23. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    you're getting a null ref from line 20, no way
     
  24. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    yeah how does that happen and how do i fix it
     
  25. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    this why you're learning how to program. programming is all about answering questions. there are many ways to do something.
     
  26. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    just remove the return; line. it's not needed
     
  27. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    ok thanks that worked
     
  28. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    line 35
    Code (CSharp):
    1. CardGame.CardBack = cardIdName;
    should change the thread name to system null reference exceptions for probably every line at this point but yeah this is also a null reference exceptions

    EDIT: sorry for taking all of your time i really appreciate what you are doing for me and also this isn't a null reference exception anymore not sure why it was before and not now didn't change anything
     
  29. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    every time i run to cursor on mono develop it tells me something different is wrong and i am unsure if it is a bug or this is normal
     
  30. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    please tell me you copied all my code.
     
  31. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
  32. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CardControllerScript : MonoBehaviour
    6. {
    7.  
    8.     private Sprite CardBack = null;
    9.     private bool matched;
    10.     public bool check;
    11.     public bool faceUp;
    12.     private CardGame tempGame;
    13.     public string cardIdName;
    14.  
    15.     void Start()
    16.     {
    17.         if(cardIdName == "" || cardIdName == null)
    18.         {
    19.             Debug.Log("Set the name in Unity, on each card");
    20.             }
    21.         tempGame = gameObject.GetComponentInParent<CardGame>();
    22.         matched = false;
    23.     }
    24.     void OnMouseDown()
    25.     {
    26.         Debug.Log("Static CardBack : " + CardGame.CardBack);
    27.         Debug.Log("cardback : " + cardIdName);
    28.         CardGame.count++;
    29.  
    30.         Debug.Log("count = " + CardGame.count);
    31.         if (CardGame.count == 1)
    32.         {
    33.             //not the same
    34.             Debug.Log("not 2 picked");
    35.             CardGame.CardBack = cardIdName;
    36.         }
    37.         else
    38.         {
    39.             // Check if they are the same
    40.             Debug.Log("check if same");
    41.             tempGame.CheckWin(cardIdName); //null reference xception
    42.         }
    43.  
    44.  
    45.  
    46.     }
    47.  
    48.     public bool GetMatched()
    49.     {
    50.         return matched;
    51.     }
    52.  
    53.     public bool GetIsFaceUp()
    54.     {
    55.         return faceUp;
    56.     }
    57.  
    58.     public void SeMatchedToTrue()
    59.     {
    60.         matched = true;
    61.     }
    62.  
    63.     public void SetCardSprite(Sprite newSprite)
    64.     {
    65.         CardBack = newSprite;
    66.     }
    67.  
    68.     public string getCardBack()
    69.     {
    70.         return cardIdName;
    71.     }
    72. }
    73.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CardGame : MonoBehaviour
    6. {
    7.  
    8.     public static string CardBack = "null";
    9.     public static int count = 0;
    10.     private CardControllerScript[] cards;
    11.     public string cardback;
    12.  
    13.     // Use this for initialization
    14.     void Start()
    15.     {
    16.         CardBack = "null";
    17.         cards = this.GetComponentsInChildren<CardControllerScript>();
    18.         Debug.Log(cards.Length);
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.  
    25.     }
    26.  
    27.     public void CheckWin(string secondCard)
    28.     {
    29.         Debug.Log("CheckWin : " + CardBack + " : " + secondCard);
    30.         if (count == 2)
    31.         {
    32.             if (CardBack == secondCard)
    33.             {
    34.                 Debug.Log("Matched!");
    35.                 count = 0;
    36.                 for (int i = 0; i < cards.Length; i++)
    37.                 {
    38.  
    39.                     if (cards[i].getCardBack() == secondCard)
    40.                     {
    41.                         Debug.Log("SeMatchedToTrue");
    42.                         cards[i].SeMatchedToTrue();
    43.                     }
    44.  
    45.                 }
    46.                 Debug.Log("you got 1 point");
    47.             }
    48.             else
    49.             {
    50.                 Debug.Log("Not match!" + cards.Length);
    51.                 count = 0;
    52.                 for (int i = 0; i < cards.Length; i++)
    53.                 {
    54.  
    55.                     if (cards[i].GetMatched() == false)
    56.                     {
    57.                         cards[i].gameObject.SetActive(true);
    58.                     }
    59.                     CardBack = "null";
    60.                 }
    61.             }
    62.         } // if count = 2
    63.     }
    64.  
    65. }
    66.  
    67.  
     
  33. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    do you have discord or any thing to chat?
     
  34. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    when i play it and i click on a card it shows this
     
  35. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    skype
    i can make a discord
     
  36. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    line 35 error,
    Did you set the string to something in unity on each card gameobject?
     
  37. Angryman100

    Angryman100

    Joined:
    Jul 19, 2017
    Posts:
    22
    it is line 41 and Do i write it in the box next to the script
    EDIT: just made a discord