NullReferenceException: Object reference not set to an instance of an object

This is the error message:
NullReferenceException: Object reference not set to an instance of an object
ClickController.Update() (at Assets/Scripts/ClickController.cs:67)

I have a card game each player has 4 decks of card to choose from. When a player draws a “Swap Card” and they choose to use it the players switch, the current player’s decks are set to false and the other players
decks are set to True. The method called is OnClickSwap and the player uses a button in the game for
this event. It worked in the past but now I get this error :frowning:
please help any help will be greatly apreciated!

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

public class ClickController : MonoBehaviour
{
    public enum Playerable { Male, Female }
    public Text maleSwapCardsText;
    public Text femaleSwapCardsText;
    public int maleSwapCards;
    public int femaleSwapCards;

    public Playerable currentPlayerTurn;
    RaycastHit raycastHit;

   
    public GameObject displayCardCanvas;
    public Text currentName;
    public Text currentDescription;
    public Image currentImage;

    public Text currentPlayerText;

    public List<Card> allSelectedCards = new List<Card>();

    public Deck deckA;
    public Deck deckB;
    public Deck deckC;
    public Deck deckD;
    public Deck deckE;
    public Deck deckF;
    public Deck deckG;
    public Deck deckH;
    public Deck blackDeck;

    public List<Deck> allDecks = new List<Deck>();


    void Start()
    {
        UpdateSwapCardsText(); // updates the number of swap cards a player has

        currentPlayerTurn = Playerable.Male; // games starts with male as first player
        UpdateTurn(currentPlayerTurn);

        ShuffleAllDecks(); // shuffles alldecks

    }

    // Update is called once per frame
    void Update()
    {
        // update black deck availability (if you have a swap card or not)
        UpdateBlackDeckAvailability();

        //update current player text
        currentPlayerText.text = "Current Player: " + currentPlayerTurn.ToString();

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        Physics.Raycast(ray, out raycastHit, 1000.0f);

        if (Input.GetMouseButtonDown(0) && displayCardCanvas.gameObject.activeSelf == false)
        {
            Debug.Log(raycastHit.collider.tag.ToString());

            if (raycastHit.collider.tag == "Deck A")
            {
                if (deckA.allCards.Count > 0)
                {
                    Debug.Log("A");

                    Card topCard = deckA.allCards[0]; // gets top card
                    allSelectedCards.Add(topCard); // adds card to YOUR deck of selected cards
                    deckA.allCards.Remove(topCard); // removes that card from the deck you chose it from

                    SelectCard(topCard); // displays cards

                    if (deckA.allCards.Count <= 0) // if no more cards, destroy the deck
                    {
                        raycastHit.collider.gameObject.SetActive(false);

                    }
                }



            }
            else if (raycastHit.collider.tag == "Deck B")
            {
                if (deckB.allCards.Count > 0)
                {
                    Debug.Log("B");

                    Card topCard = deckB.allCards[0];
                    allSelectedCards.Add(topCard);
                    deckB.allCards.Remove(topCard);

                    SelectCard(topCard);

                    if (deckB.allCards.Count <= 0)
                    {
                        raycastHit.collider.gameObject.SetActive(false);

                    }
                }




            }
            else if (raycastHit.collider.tag == "Deck C")
            {
                if (deckC.allCards.Count > 0)
                {
                    Debug.Log("C");

                    Card topCard = deckC.allCards[0];
                    allSelectedCards.Add(topCard);
                    deckC.allCards.Remove(topCard);

                    SelectCard(topCard);

                    if (deckC.allCards.Count <= 0)
                    {
                        raycastHit.collider.gameObject.SetActive(false);

                    }
                }



            }
            else if (raycastHit.collider.tag == "Deck D")
            {
                if (deckD.allCards.Count > 0)
                {
                    Debug.Log("D");

                    Card topCard = deckD.allCards[0];
                    allSelectedCards.Add(topCard);
                    deckD.allCards.Remove(topCard);

                    SelectCard(topCard);

                    if (deckD.allCards.Count <= 0)
                    {
                        raycastHit.collider.gameObject.SetActive(false);

                    }
                }
            }
            else if (raycastHit.collider.tag == "Deck E")
            {
                if (deckE.allCards.Count > 0)
                {
                    Debug.Log("E");

                    Card topCard = deckE.allCards[0];
                    allSelectedCards.Add(topCard);
                    deckE.allCards.Remove(topCard);

                    SelectCard(topCard);

                    if (deckE.allCards.Count <= 0)
                    {
                        raycastHit.collider.gameObject.SetActive(false);

                    }
                }
            }
            else if (raycastHit.collider.tag == "Deck F")
            {
                if (deckF.allCards.Count > 0)
                {
                    Debug.Log("F");

                    Card topCard = deckF.allCards[0];
                    allSelectedCards.Add(topCard);
                    deckF.allCards.Remove(topCard);

                    SelectCard(topCard);

                    if (deckF.allCards.Count <= 0)
                    {
                        raycastHit.collider.gameObject.SetActive(false);

                    }
                }
            }
            else if (raycastHit.collider.tag == "Deck G")
            {
                if (deckG.allCards.Count > 0)
                {
                    Debug.Log("G");

                    Card topCard = deckG.allCards[0];
                    allSelectedCards.Add(topCard);
                    deckG.allCards.Remove(topCard);

                    SelectCard(topCard);

                    if (deckG.allCards.Count <= 0)
                    {
                        raycastHit.collider.gameObject.SetActive(false);

                    }
                }
            }
            else if (raycastHit.collider.tag == "Deck H")
            {
                if (deckH.allCards.Count > 0)
                {
                    Debug.Log("H");

                    Card topCard = deckH.allCards[0];
                    allSelectedCards.Add(topCard);
                    deckH.allCards.Remove(topCard);

                    SelectCard(topCard);

                    if (deckH.allCards.Count <= 0)
                    {
                        raycastHit.collider.gameObject.SetActive(false);

                    }
                }

            }
            else if (raycastHit.collider.tag == "Black Deck")
            {
                if (blackDeck.allCards.Count > 0)
                {
                    if ((currentPlayerTurn == Playerable.Male && maleSwapCards > 0))
                    {
                        Debug.Log("Black");

                        Card topCard = blackDeck.allCards[0];
                        allSelectedCards.Add(topCard);
                        blackDeck.allCards.Remove(topCard);

                        SelectCard(topCard);

                        if (blackDeck.allCards.Count <= 0)
                        {
                            raycastHit.collider.gameObject.SetActive(false);

                        }
                    }
                    else if (currentPlayerTurn == Playerable.Female && femaleSwapCards > 0)
                    {
                        Debug.Log("Black");

                        Card topCard = blackDeck.allCards[0];
                        allSelectedCards.Add(topCard);
                        blackDeck.allCards.Remove(topCard);

                        SelectCard(topCard);

                        if (blackDeck.allCards.Count <= 0)
                        {
                            raycastHit.collider.gameObject.SetActive(false);

                        }
                    }
                }

            }
        }
    }

    public void SelectCard(Card card)
    {
        displayCardCanvas.SetActive(true);
        currentName.text = card.cardName;
        currentDescription.text = card.cardDescription;
        currentImage.sprite = card.cardSprite;

        CheckCard(card); // check if swap card
        UpdateSwapCardsText();
    }

    public void OnClickDone()
    {
        displayCardCanvas.SetActive(false);

    }

    public void OnClickSwap() // when you click swap
    {

        Debug.Log("Swap card used");
        if (currentPlayerTurn == Playerable.Male && maleSwapCards > 0 || currentPlayerTurn == Playerable.Female && femaleSwapCards > 0)
        {
            if ((currentPlayerTurn == Playerable.Male && maleSwapCards > 0))
            {
                currentPlayerTurn = Playerable.Female;
                maleSwapCards--;
            }
            else if (currentPlayerTurn == Playerable.Female && femaleSwapCards > 0)
            {
                femaleSwapCards--;
            }

            UpdateSwapCardsText();


            if (currentPlayerTurn == Playerable.Male)
            {
                currentPlayerTurn = Playerable.Female;
                UpdateTurn(currentPlayerTurn);
            }
            else if (currentPlayerTurn == Playerable.Female)
            {
                currentPlayerTurn = Playerable.Male;
                UpdateTurn(currentPlayerTurn);
            }
        }

        ShuffleAllDecks();
    }

    public void UpdateTurn(Playerable player) // used to turn on/off decks
    {
        if (player == Playerable.Male)
        {
            deckA.gameObject.SetActive(true);
            deckB.gameObject.SetActive(true);
            deckC.gameObject.SetActive(true);
            deckD.gameObject.SetActive(true);
            deckE.gameObject.SetActive(false);
            deckF.gameObject.SetActive(false);
            deckG.gameObject.SetActive(false);
            deckH.gameObject.SetActive(false);

        }
        else if (player == Playerable.Female)
        {
            deckA.gameObject.SetActive(false);
            deckB.gameObject.SetActive(false);
            deckC.gameObject.SetActive(false);
            deckD.gameObject.SetActive(false);
            deckE.gameObject.SetActive(true);
            deckF.gameObject.SetActive(true);
            deckG.gameObject.SetActive(true);
            deckH.gameObject.SetActive(true);
        }
    }

    public void CheckCard(Card currentCard)
    {
        if (currentCard.cardtype == Card.CardTypable.Swap)
        {
            if (currentPlayerTurn == Playerable.Male)
            {
                maleSwapCards++;
            }
            else
            {
                femaleSwapCards++;
            }
        }
    }

    public void UpdateSwapCardsText()
    {
        maleSwapCardsText.text = "Swap Cards: " + maleSwapCards;
        femaleSwapCardsText.text = "Swap Cards: " + femaleSwapCards;
    }

    public void UpdateBlackDeckAvailability()
    {
        // black deck
        if (currentPlayerTurn == Playerable.Male)
        {
            if (maleSwapCards > 0)
            {
                blackDeck.gameObject.SetActive(true);

            }
            else
            {
                blackDeck.gameObject.SetActive(false);

            }
        }

        if (currentPlayerTurn == Playerable.Female)
        {
            if (femaleSwapCards > 0)
            {
                blackDeck.gameObject.SetActive(true);

            }
            else
            {
                blackDeck.gameObject.SetActive(false);

            }
        }
      
    }

    public void ShuffleAllDecks()
    {
        for (int i = 0; i < allDecks.Count; i++)
        {
            for (int j = 0; j < allDecks[i].allCards.Count; j++)
            {
                Card temp = allDecks[i].allCards[j];
                int randomIndex = Random.Range(j, allDecks[i].allCards.Count);
                allDecks[i].allCards[j] = allDecks[i].allCards[randomIndex];
                allDecks[i].allCards[randomIndex] = temp;
            }
        }
    }
}

Null reference? Always the same answer. Always. It’s the most common error.

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

1 Like