Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Null Reference exception - but I use the function elsewhere in the same way!

Discussion in 'Getting Started' started by thisiswater, Mar 17, 2021.

  1. thisiswater

    thisiswater

    Joined:
    Apr 28, 2016
    Posts:
    5
    Hi guys,

    New to unity and c#, I'm having trouble with a null reference exception which I have nailed down to a single line but I can't see why it's occurring.

    I am trying to add a gameObject to a dictionary at a given key by invoking a function. When I do so with another object in the same way (as far as I can see) it works fine, but I'm getting a null reference exception when I try with another that I don't know how to diagnose. I've narrowed it down to the line that's producing the null reference, but my efforts to fix it haven't gotten anywhere. Probably very simple for someone more familiar!

    Line 11 in my entityAction script causes the exception. I've tried to cut out irrelevant code.

    Code (CSharp):
    1. public class entityAction : MonoBehaviour
    2. {
    3.  
    4.     private Movement _movement;
    5.     private GameController _gameController;
    6.  
    7.     void Start()
    8.     {
    9.         _gameController = GameObject.Find("GameManager").GetComponent<GameController>();
    10.         _movement = this.GetComponent<Movement>();
    11.         _gameController.addToQueue(1, this.gameObject);
    12.     }
    13. }
    Code (CSharp):
    1.  
    2. public class GameController : MonoBehaviour
    3. {
    4.     [HideInInspector]
    5.     public int globalTime;
    6.     public bool playerTurn;
    7.     public Dictionary<int, GameObject> turnQueue;
    8.  
    9.     private GameObject player;
    10.  
    11.     void Start()
    12.     {
    13.         globalTime = 0;
    14.         playerTurn = true;
    15.         turnQueue = new Dictionary<int, GameObject>();
    16.         player = GameObject.Find("Wanderer");
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         if (!playerTurn)
    22.         {
    23.             globalTime++;
    24.             if(turnQueue.ContainsKey(globalTime))
    25.             {
    26.                 Debug.Log(turnQueue[globalTime].name);
    27.                 if (turnQueue[globalTime] == player)
    28.                 {
    29.                     playerTurn = true;
    30.                 }
    31.                 else
    32.                 {
    33.                     Debug.Log("entityTurn");
    34.                     turnQueue[globalTime].GetComponent<entityAction>().Act();
    35.                 }
    36.             }
    37.  
    38.         }
    39.     }
    40.  
    41.     public void addToQueue(int inTime, GameObject entity)
    42.     {
    43.         if(!turnQueue.ContainsKey(globalTime + inTime))
    44.         {
    45.             turnQueue[globalTime + inTime] = entity;
    46.         }
    47.         else
    48.         {
    49.             addToQueue(inTime + 1, entity);
    50.         }
    51.     }
    52. }
    53.  
    54.  
    55.  
    Help would be greatly appreciated, I am still learning.
     
    Last edited: Mar 17, 2021
  2. thisiswater

    thisiswater

    Joined:
    Apr 28, 2016
    Posts:
    5
    I probably didn't add enough information:

    Here is the script which contains the Move() function, also attached to the same gameobject.

    Code (CSharp):
    1. public class Movement : MonoBehaviour
    2. {
    3.     private GameController _gameController;
    4.     private Transform toMove;
    5.     private BaseStats _baseStats;
    6.  
    7.     void Start()
    8.     {
    9.         toMove = this.transform;
    10.         _gameController = GameObject.Find("GameManager").GetComponent<GameController>();
    11.     }
    12.  
    13.  
    14.     public void Move(Vector3 direction)
    15.     {
    16.  
    17.         toMove.position += direction;
    18.         Debug.Log(this.gameObject.name);
    19.         _gameController.addToQueue(100, this.gameObject);
    20.     }
    21.  
    22. }
     
  3. thisiswater

    thisiswater

    Joined:
    Apr 28, 2016
    Posts:
    5
    Update: Didn't quite solve it but found a workaround. Still not quite sure what was happening.