2017-04-20 204 views
0

感謝您的閱讀,我正在使用Unity編寫C#中的小型存儲卡遊戲。當我運行某個場景時,我收到了不斷的錯誤。 的錯誤是如下:Unity問題,NullReferenceException:對象引用未設置爲對象的實例

"NullReferenceException: Object reference not set to an instance of an object 
Card.SetUpArt() (at Assets/Scripts/Card.cs:31) 
Pairs.SetUpDeck() (at Assets/Scripts/Pairs.cs:62) 
Pairs.Update() (at Assets/Scripts/Pairs.cs:23)" 

我的代碼是:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine.UI; 
using UnityEngine; 

public class Card : MonoBehaviour { 

    public static bool NO_TURN = false; 

    [SerializeField] 
    private int cardState; //state of card 
    [SerializeField] 
    private int cardNumber; //Card value (1-13) 
    [SerializeField] 
    private bool _setUp = false; 

    private Sprite cBack; //card back (Green square) 
    private Sprite cFace; //card face (1-10 JQKA) 

    private GameObject pairsManager; 

    void Begin() 
    { 
     cardState = 1; //cards face down 
     pairsManager = GameObject.FindGameObjectWithTag("PairsManager"); 

    } 

    public void SetUpArt() 
    { 
     cBack = pairsManager.GetComponent<Pairs>().GetBack(); //<--error 
     cFace = pairsManager.GetComponent<Pairs>().GetFace(cardNumber); 

     turnCard();//turns the card 
    } 

    public void turnCard() //handles turning of card 
    { 
     if (cardState == 0) 
     { 
      cardState = 1; 
     } 
     else if(cardState == 1) 
     { 
      cardState = 0; 
     } 
     if (cardState == 0 && !NO_TURN) 
     { 
      GetComponent<Image>().sprite = cBack; // shows card back 
     } 
     else if (cardState == 1 && !NO_TURN) 
     { 
      GetComponent<Image>().sprite = cFace; // shows card front 
     } 
    } 

    //setters and getters 

    public int Number 
    { 
     get {return cardNumber;} 
     set { cardNumber = value;} 
    } 

    public int State 
    { 
     get { return cardState; } 
     set { cardState = value; } 
    } 

    public bool SetUp 
    { 
     get { return _setUp; } 
     set { _setUp = value; } 
    } 


    public void PairCheck() 
    { 
     StartCoroutine(pause()); 
    } 

    IEnumerator pause() 
    { 
     yield return new WaitForSeconds(1); 
     if (cardState == 0) 
     { 
      GetComponent<Image>().sprite = cBack; 
     } 
     else if (cardState == 1) 
     { 
      GetComponent<Image>().sprite = cFace; 
     } 
    } 
} 

和:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine.UI; 
using UnityEngine.SceneManagement; 
using UnityEngine; 

public class Pairs : MonoBehaviour { 

    public Sprite[] cardFace; //array of card faces 
    public Sprite cardBack; 
    public GameObject[] deck; //array of deck 
    public Text pairsCount; 


    private bool deckSetUp = false; 
    private int pairsLeft = 13; 


    // Update is called once per frame 
    void Update() { 
     if (!deckSetUp) 
     { 
      SetUpDeck(); 
     } 
     if (Input.GetMouseButtonUp(0)) //detects left click 
     { 
      CheckDeck(); 
     } 
    }//Update 

    void SetUpDeck() 
    { 

     for(int i = 0; i <deck.Length; i++)//resets cards 
     { 
      deck[i].GetComponent<Card>().SetUp = false; 
     } 

     for (int ix = 0; ix < 2; ix++) //sets up cards twice, 
     { 
      for(int i = 1; i < 14; i++)//sets up card value (2-10 JQKA) 
      { 
       bool test = false; 
       int val = 0; 
       while (!test) 
       { 
        val = Random.Range(0, deck.Length); 
        test = !(deck[val].GetComponent<Card>().SetUp); 
       }//while 

       //sets up cards 

       deck[val].GetComponent<Card>().Number = i; 
       deck[val].GetComponent<Card>().SetUp = true; 

      }//nested for 

     }//for 

     foreach (GameObject crd in deck) 
     { 
      crd.GetComponent<Card>().SetUpArt(); 
     } 

     if (!deckSetUp) 
     { 
      deckSetUp = true; 
     } 
    }//SetUpDeck 

    public Sprite GetBack() 
    { 
     return cardBack; 
    }//getBack 

    public Sprite GetFace(int i) 
    { 
     return cardFace[i - 1]; 

    }//getFace 

    void CheckDeck() 
    { 
     List <int> crd = new List<int>(); 

     for(int i = 0; i < deck.Length; i++) 
     { 
      if(deck[i].GetComponent<Card>().State == 1) 
      { 
       crd.Add(i); 
      } 

     } 

     if(crd.Count == 2) 
     { 
      CompareCards(crd); 
     } 
    }//CheckDeck 

    void CompareCards(List<int> crd) 
    { 
     Card.NO_TURN = true; //stops cards turning 

     int x = 0; 

     if(deck[crd[0]].GetComponent<Card>().Number == 
      deck[crd[1]].GetComponent<Card>().Number) 
     { 
      x = 2; 
      pairsLeft--; 
      pairsCount.text = "PAIRS REMAINING: " + pairsLeft; 

      if(pairsLeft == 0) // goes to home screen when game has been won 
      { 
       SceneManager.LoadScene("Home"); 
      } 

     } 

     for(int j = 0; j < crd.Count; j++) 
     { 
      deck[crd[j]].GetComponent<Card>().State = x; 
      deck[crd[j]].GetComponent<Card>().PairCheck(); 

     } 

    }//CompareCards 
} 

任何幫助,任何人都可以提供將不勝感激。先謝謝你。 Link to GitHub Repository

+0

錯誤表明'pairsManager'沒有一個'pairs'組件在它試圖在'SetUpArt()'中檢索它時。或者,如果'GameObject.FindGameObjectWithTag()'未能檢索到'',那麼''''''''''''''' – Serlite

+0

謝謝,我對Unity非常陌生,並且仍在學習。如何將「Pairs」組件添加到pairsManager中? –

+0

您可能希望使用編輯器界面中的添加組件按鈕來添加「Pairs」的實例。雖然如果你的情況變得更加複雜,而你仍然無法解決它,那麼考慮發佈一個新問題。 – Serlite

回答

相關問題