Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Null reference exception from list.

Discussion in 'Scripting' started by xxcode1400xx, Apr 9, 2023.

  1. xxcode1400xx

    xxcode1400xx

    Joined:
    Mar 11, 2023
    Posts:
    5
    Hi I have a script with a list containing game objects to be added later. I am trying to access this list in another script but it is throwing a null reference exception error. Even checking if list is null throws an error. Please help.

    1st script:
    Code (CSharp):
    1. public List<GameObject> walkablePathObjects = new List <GameObject>();
    2nd script:
    Code (CSharp):
    1. [SerializeField] PathfindingToolScript pathfindingToolScript;
    Code (CSharp):
    1. if (pathfindingToolScript.walkablePathObjects == null){
    2.             return;
    3.         }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    If your if checking is throwing null, you should be debugging.

    1. Find out what is null
    2. Find out why it's null
    3. Fix it!

    So, is pathfindingToolScript null?
     
    Kurt-Dekker likes this.
  3. xxcode1400xx

    xxcode1400xx

    Joined:
    Mar 11, 2023
    Posts:
    5
    It was lol, my apologies. The simplest of things get overlooked.