[Solved] NullReference Exception for unknown reason

I can’t find out why my code always return this error, can someone maybe read over my code an tell me why? (Sorry if its obvious, I’m new to coding)
I have 3 classes so far:

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

public class GameManager_Master : MonoBehaviour {

    public delegate void GameEventHandler();
    public event GameEventHandler EventGameStart;

    public delegate void CardEventHandler(int[] cards, int player);
    public event CardEventHandler EventFirst4CardsDealt;
    public event CardEventHandler EventAllCardsDealt;

    public void CallEventGameStart()
    {
        EventGameStart();
    }

    public void CallEventFirst4CardsDealt(int[] cards, int player)
    {
        EventFirst4CardsDealt(cards, player);
    }

    public void CallEventAllCardsDealt(int[] cards, int player)
    {
        EventAllCardsDealt(cards, player);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager_Control : MonoBehaviour {

    private GameManager_Master gmMaster;

    void OnEnable () {
        SetInitRefs();
    }
   
    void Update () {
       if(Input.GetButtonDown("Start Game"))
        {
            gmMaster.CallEventGameStart();
            Debug.Log("EventGameStartCalled");
        }
    }

    void SetInitRefs()
    {
        gmMaster = GetComponent<GameManager_Master>();
    }
}
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class GameManager_DealCards : MonoBehaviour {

    int dummy;

    float dealtime;

    int[] cards = Enumerable.Range(1, 32).ToArray();

    public float dealTime;

    public List<GameObject> players;

    GameManager_Master gmMaster;

    int[] cards1;
    int[] cards2;

    int[] cards3;
    int[] cards4;
    int[] cards5;
    int[] cards6;

    public int[] cards7;
    public int[] cards8;
    public int[] cards9;
    public int[] cards10;
    public int[] cards11;
    public int[] cards12;
    public int[] cards13;
    public int[] cards14;

    void OnEnable()
    {
        SetInitRefs();
    }

    void OnDisable()
    {
       
    }

    void SetInitRefs()
    {
        gmMaster = GetComponent<GameManager_Master>();
        dummy = 0;
    }

    void shuffle(int[] cards)
    {
        for (int t = 0; t < cards.Length; t++)
        {
            int tmp = cards[t];
            int r = Random.Range(t, cards.Length);
            cards[t] = cards[r];
            cards[r] = tmp;
        }
    }

    void MixCards()
    {
        shuffle(cards);
        SplitArray(cards, out cards1, out cards2);
        SplitArray(cards1, out cards3, out cards4);
        SplitArray(cards2, out cards5, out cards6);
        SplitArray(cards3, out cards7, out cards8);
        SplitArray(cards4, out cards9, out cards10);
        SplitArray(cards5, out cards11, out cards12);
        SplitArray(cards6, out cards13, out cards14);
    }

    void SplitArray(int[] array, out int[] firstArray, out int[] secondArray)
    {
        firstArray = array.Take(array.Length / 2).ToArray();
        secondArray = array.Skip(array.Length / 2).ToArray();
    }

    void DealCards()
    {
        if(dummy == 0)
        {
            gmMaster.CallEventFirst4CardsDealt(cards7, 1);
            gmMaster.CallEventFirst4CardsDealt(cards8, 2);
            gmMaster.CallEventFirst4CardsDealt(cards9, 3);
            gmMaster.CallEventFirst4CardsDealt(cards10, 4);

        }
        else if(dummy == 1)
        {
            Wait(dealTime);
            gmMaster.CallEventFirst4CardsDealt(cards11, 1);
            gmMaster.CallEventFirst4CardsDealt(cards12, 2);
            gmMaster.CallEventFirst4CardsDealt(cards13, 3);
            gmMaster.CallEventFirst4CardsDealt(cards14, 4);

        }
        else
        {
            dummy = 0;
        }
    }

    IEnumerator Wait(float t)
    {
        yield return new WaitForSeconds(t);
    }

}

Here is the error:
NullReferenceException: Object reference not set to an instance of an object
GameManager_Master.CallEventGameStart () (at Assets/Scripts/GameManager_Master.cs:16)
GameManager_Control.Update () (at Assets/Scripts/GameManager_Control.cs:16)

Thanks in Advance

public List<GameObject> players = new List<GameObject>();

What do you mean by that? I tried what you suggested but unfortunatly it didnt help, neither did removing the code entirely.

Try coffee. NULL and 0 is not the same, you need a instance of the list.

Sorry if I’m not getting your point, but that line of code has nothing to do with the problem, I’m not using it, I’ve removed it from the code and it still isn’t working

Is the GameManager_Master component attached to the same game object as GameManager_Control is?

Yes it is

Your not setting your variables to anything, but trying to use them. The delegates in GameManager_Master dont need event before them.

In GameManager_Control add this assuming all these scripts are on the same object:

void Start()
{
gmMaster = GetComponent<GameMaster_Manager>();
}

If they arent on the same gameobject you will want to get a reference to the gameobject that has it then call that on the referenced gameobject instead.

I believe invoking an event to which no one has yet subscribed will cause an NullReferenceException. I had problems in the past with events and script execution order. Try to create an if statement for null check.

1 Like

But I have already made this reference in GameManager_Control, it’s in the Method SetInitRefs() which is called in OnEnable()

Yes thank you, that solved my problem!

1 Like

The error points to codeline 16 which is the List that is not made a instance. hence the NULL reference.

1 Like

gmMaster.CallEventGameStart(); What is inside here?

EventGameStart(); kaskiU has already solved the problem though. Thanks for the help though!