Search Unity

Resolved NullReferenceException Console Error

Discussion in 'Scripting' started by TeacupBandit, Jul 14, 2021.

  1. TeacupBandit

    TeacupBandit

    Joined:
    May 5, 2020
    Posts:
    3
    I cannot figure out what is causing this NullReferenceException Console error. Hoping someone can spot my misstep. I believe it could be a difference in scripting based on different unity versions possibly, but I am unsure.

    Unity Version: 2019.4.15f1 Personal

    I am working through this tutorial:


    The error occurs when attempting to get the neighboring blocks to become red, approx 20:00 in the video.

    I am receiving this console error:
    NullReferenceException: Object reference not set to an instance of an object
    TacticsMove.ComputeAdjacencyLists () (at Assets/Scripts/TacticsMove.cs:53)
    TacticsMove.FindSelectableTiles () (at Assets/Scripts/TacticsMove.cs:59)
    PlayerMove.Update () (at Assets/Scripts/PlayerMove.cs:16)

    These are my 3 scripts:

    TacticsMove.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TacticsMove : MonoBehaviour
    6. {
    7.     List<Tile> selectableTiles = new List<Tile>();
    8.     GameObject[] tiles;
    9.  
    10.     Stack<Tile> path = new Stack<Tile>();
    11.     Tile currentTile;
    12.  
    13.     public int move = 5;
    14.     public float jumpHeight = 2;
    15.     public float moveSpeed = 2;
    16.  
    17.     Vector3 velocity = new Vector3();
    18.     Vector3 heading = new Vector3();
    19.  
    20.     float halfHeight = 0;
    21.  
    22.     protected void Init()
    23.     {
    24.         tiles = GameObject.FindGameObjectsWithTag("Tile");
    25.  
    26.         halfHeight = GetComponent<Collider>().bounds.extents.y;
    27.     }
    28.  
    29.     public void GetCurrentTile()
    30.     {
    31.         currentTile = GetTargetTile(gameObject);
    32.         currentTile.current = true;
    33.     }
    34.  
    35.     public Tile GetTargetTile(GameObject target)
    36.     {
    37.         RaycastHit hit;
    38.         Tile tile = null;
    39.  
    40.         if (Physics.Raycast(target.transform.position, -Vector3.up, out hit, 1))
    41.         {
    42.             tile = hit.collider.GetComponent<Tile>();
    43.         }
    44.  
    45.         return tile;
    46.     }
    47.  
    48.     public void ComputeAdjacencyLists()
    49.     {
    50.         foreach (GameObject tile in tiles)
    51.         {
    52.             Tile t = tile.GetComponent<Tile>();
    53.             t.FindNeighbors(jumpHeight);
    54.         }
    55.     }
    56.  
    57.     public void FindSelectableTiles()
    58.     {
    59.         ComputeAdjacencyLists();
    60.         GetCurrentTile();
    61.  
    62.         Queue<Tile> process = new Queue<Tile>();
    63.  
    64.         process.Enqueue(currentTile);
    65.         currentTile.visited = true;
    66.         //currentTile.parent = ??
    67.  
    68.         while (process.Count > 0)
    69.         {
    70.             Tile t = process.Dequeue();
    71.  
    72.             selectableTiles.Add(t);
    73.             t.selectable = true;
    74.  
    75.             if (t.distance < move)
    76.             {
    77.  
    78.                 foreach (Tile tile in t.adjacencyList)
    79.                 {
    80.                     if (!tile.visited)
    81.                     {
    82.                         tile.parent = t;
    83.                         tile.visited = true;
    84.                         tile.distance = 1 + t.distance;
    85.                         process.Enqueue(tile);
    86.                     }
    87.                 }
    88.             }
    89.         }
    90.     }
    91.  
    92. }
    93.  
    Tile.cs - applied to each tile block
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Tile : MonoBehaviour
    6. {
    7.  
    8.     public bool walkable = true;
    9.     public bool current = false;
    10.     public bool target = false;
    11.     public bool selectable = false;
    12.  
    13.     public List<Tile> adjacencyList = new List<Tile>();
    14.  
    15.  
    16.     // Needed BFS (Breadth First Search)
    17.     public bool visited = false;
    18.     public Tile parent = null;
    19.     public int distance = 0;
    20.  
    21.  
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.        
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         if (current)
    33.         {
    34.             GetComponent<Renderer>().material.color = Color.magenta;
    35.         }
    36.  
    37.         else if (target)
    38.         {
    39.             GetComponent<Renderer>().material.color = Color.green;
    40.         }
    41.  
    42.         else if (selectable)
    43.         {
    44.             GetComponent<Renderer>().material.color = Color.yellow;
    45.         }
    46.  
    47.         else
    48.         {
    49.             GetComponent<Renderer>().material.color = Color.white;
    50.         }
    51.     }
    52.  
    53.     public void Reset()
    54.     {
    55.         adjacencyList.Clear();
    56.  
    57.         current = false;
    58.         target = false;
    59.         selectable = false;
    60.  
    61.         visited = false;
    62.         parent = null;
    63.         distance = 0;
    64.     }
    65.  
    66.     public void FindNeighbors(float jumpHeight)
    67.     {
    68.         Reset();
    69.  
    70.         CheckTile(Vector3.right, jumpHeight);
    71.         CheckTile(-Vector3.right, jumpHeight);
    72.         CheckTile(Vector3.forward, jumpHeight);
    73.         CheckTile(-Vector3.forward, jumpHeight);
    74.     }
    75.  
    76.     public void CheckTile(Vector3 direction, float jumpHeight)
    77.     {
    78.         Vector3 halfExtents = new Vector3(0.25f, (1+ jumpHeight) / 2.0f, 0.25f);
    79.         Collider[] colliders = Physics.OverlapBox(transform.position + direction, halfExtents);
    80.  
    81.         foreach (Collider item in colliders)
    82.         {
    83.             Tile tile = item.GetComponent<Tile>();
    84.             if (tile != null && tile.walkable)
    85.             {
    86.                 RaycastHit hit;
    87.  
    88.                 if (!Physics.Raycast(tile.transform.position, Vector3.up, out hit, 1))
    89.                 {
    90.                     adjacencyList.Add(tile);
    91.                 }
    92.             }
    93.         }
    94.     }
    95. }
    96.  
    PlayerMove.cs - attached to player capsule
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMove : TacticsMove
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         Init();
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         FindSelectableTiles();
    17.     }
    18. }
    19.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    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.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
  3. TeacupBandit

    TeacupBandit

    Joined:
    May 5, 2020
    Posts:
    3
    Maybe that's what I am struggling with, because when looking for the "cookie", I see the cookie. Its in my code. Or maybe I am wrong, which is why I am here. Essentially from what I researched for hours (before working up the sweat to post here for the first time ever) was trying to find out what I was missing. And from what I can tell, I just don't see what is missing.

    Sorry I just am so lost and trying to keep that momentum.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Code (CSharp):
    1.             Tile t = tile.GetComponent<Tile>();
    2.             t.FindNeighbors(jumpHeight);
    Your error is here and judging by this code it means that at least one of the
    tile
    objects in your
    tiles
    array doesn't have a
    Tile
    component attached to it.

    Look through all the objects with the "Tile" tag in your scene and make sure they have the Tile component attached.
     
  5. TeacupBandit

    TeacupBandit

    Joined:
    May 5, 2020
    Posts:
    3
    I thought about what you said and I just completely rebuilt the "Tile" objects and its working now!