Search Unity

[Solved] NullReference Exception for unknown reason

Discussion in 'Scripting' started by Boardaic, Aug 20, 2017.

  1. Boardaic

    Boardaic

    Joined:
    Jan 25, 2017
    Posts:
    15
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager_Master : MonoBehaviour {
    6.  
    7.     public delegate void GameEventHandler();
    8.     public event GameEventHandler EventGameStart;
    9.  
    10.     public delegate void CardEventHandler(int[] cards, int player);
    11.     public event CardEventHandler EventFirst4CardsDealt;
    12.     public event CardEventHandler EventAllCardsDealt;
    13.  
    14.     public void CallEventGameStart()
    15.     {
    16.         EventGameStart();
    17.     }
    18.  
    19.     public void CallEventFirst4CardsDealt(int[] cards, int player)
    20.     {
    21.         EventFirst4CardsDealt(cards, player);
    22.     }
    23.  
    24.     public void CallEventAllCardsDealt(int[] cards, int player)
    25.     {
    26.         EventAllCardsDealt(cards, player);
    27.     }
    28. }
    29.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager_Control : MonoBehaviour {
    6.  
    7.     private GameManager_Master gmMaster;
    8.  
    9.     void OnEnable () {
    10.         SetInitRefs();
    11.     }
    12.    
    13.     void Update () {
    14.        if(Input.GetButtonDown("Start Game"))
    15.         {
    16.             gmMaster.CallEventGameStart();
    17.             Debug.Log("EventGameStartCalled");
    18.         }
    19.     }
    20.  
    21.     void SetInitRefs()
    22.     {
    23.         gmMaster = GetComponent<GameManager_Master>();
    24.     }
    25. }
    26.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using UnityEngine;
    5.  
    6. public class GameManager_DealCards : MonoBehaviour {
    7.  
    8.     int dummy;
    9.  
    10.     float dealtime;
    11.  
    12.     int[] cards = Enumerable.Range(1, 32).ToArray();
    13.  
    14.     public float dealTime;
    15.  
    16.     public List<GameObject> players;
    17.  
    18.     GameManager_Master gmMaster;
    19.  
    20.     int[] cards1;
    21.     int[] cards2;
    22.  
    23.     int[] cards3;
    24.     int[] cards4;
    25.     int[] cards5;
    26.     int[] cards6;
    27.  
    28.     public int[] cards7;
    29.     public int[] cards8;
    30.     public int[] cards9;
    31.     public int[] cards10;
    32.     public int[] cards11;
    33.     public int[] cards12;
    34.     public int[] cards13;
    35.     public int[] cards14;
    36.  
    37.     void OnEnable()
    38.     {
    39.         SetInitRefs();
    40.     }
    41.  
    42.     void OnDisable()
    43.     {
    44.        
    45.     }
    46.  
    47.     void SetInitRefs()
    48.     {
    49.         gmMaster = GetComponent<GameManager_Master>();
    50.         dummy = 0;
    51.     }
    52.  
    53.     void shuffle(int[] cards)
    54.     {
    55.         for (int t = 0; t < cards.Length; t++)
    56.         {
    57.             int tmp = cards[t];
    58.             int r = Random.Range(t, cards.Length);
    59.             cards[t] = cards[r];
    60.             cards[r] = tmp;
    61.         }
    62.     }
    63.  
    64.     void MixCards()
    65.     {
    66.         shuffle(cards);
    67.         SplitArray(cards, out cards1, out cards2);
    68.         SplitArray(cards1, out cards3, out cards4);
    69.         SplitArray(cards2, out cards5, out cards6);
    70.         SplitArray(cards3, out cards7, out cards8);
    71.         SplitArray(cards4, out cards9, out cards10);
    72.         SplitArray(cards5, out cards11, out cards12);
    73.         SplitArray(cards6, out cards13, out cards14);
    74.     }
    75.  
    76.     void SplitArray(int[] array, out int[] firstArray, out int[] secondArray)
    77.     {
    78.         firstArray = array.Take(array.Length / 2).ToArray();
    79.         secondArray = array.Skip(array.Length / 2).ToArray();
    80.     }
    81.  
    82.     void DealCards()
    83.     {
    84.         if(dummy == 0)
    85.         {
    86.             gmMaster.CallEventFirst4CardsDealt(cards7, 1);
    87.             gmMaster.CallEventFirst4CardsDealt(cards8, 2);
    88.             gmMaster.CallEventFirst4CardsDealt(cards9, 3);
    89.             gmMaster.CallEventFirst4CardsDealt(cards10, 4);
    90.  
    91.         }
    92.         else if(dummy == 1)
    93.         {
    94.             Wait(dealTime);
    95.             gmMaster.CallEventFirst4CardsDealt(cards11, 1);
    96.             gmMaster.CallEventFirst4CardsDealt(cards12, 2);
    97.             gmMaster.CallEventFirst4CardsDealt(cards13, 3);
    98.             gmMaster.CallEventFirst4CardsDealt(cards14, 4);
    99.  
    100.         }
    101.         else
    102.         {
    103.             dummy = 0;
    104.         }
    105.     }
    106.  
    107.     IEnumerator Wait(float t)
    108.     {
    109.         yield return new WaitForSeconds(t);
    110.     }
    111.  
    112. }
    113.  
    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
     
  2. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    Code (CSharp):
    1. public List<GameObject> players = new List<GameObject>();
     
  3. Boardaic

    Boardaic

    Joined:
    Jan 25, 2017
    Posts:
    15
    What do you mean by that? I tried what you suggested but unfortunatly it didnt help, neither did removing the code entirely.
     
  4. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    Try coffee. NULL and 0 is not the same, you need a instance of the list.
     
  5. Boardaic

    Boardaic

    Joined:
    Jan 25, 2017
    Posts:
    15
    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
     
  6. kaskiU

    kaskiU

    Joined:
    Aug 6, 2013
    Posts:
    50
    Is the GameManager_Master component attached to the same game object as GameManager_Control is?
     
  7. Boardaic

    Boardaic

    Joined:
    Jan 25, 2017
    Posts:
    15
    Yes it is
     
  8. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    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:

    Code (CSharp):
    1. void Start()
    2. {
    3. gmMaster = GetComponent<GameMaster_Manager>();
    4. }
    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.
     
  9. kaskiU

    kaskiU

    Joined:
    Aug 6, 2013
    Posts:
    50
    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.
     
    Boardaic likes this.
  10. Boardaic

    Boardaic

    Joined:
    Jan 25, 2017
    Posts:
    15
    But I have already made this reference in GameManager_Control, it's in the Method SetInitRefs() which is called in OnEnable()
     
  11. Boardaic

    Boardaic

    Joined:
    Jan 25, 2017
    Posts:
    15
    Yes thank you, that solved my problem!
     
    kaskiU likes this.
  12. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    The error points to codeline 16 which is the List that is not made a instance. hence the NULL reference.
     
    Boardaic likes this.
  13. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    gmMaster.CallEventGameStart(); What is inside here?
     
  14. Boardaic

    Boardaic

    Joined:
    Jan 25, 2017
    Posts:
    15
    EventGameStart(); kaskiU has already solved the problem though. Thanks for the help though!