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. Dismiss Notice

Question NullReferenceException but don't know why?

Discussion in 'Scripting' started by MerlinG2, Aug 25, 2021.

Thread Status:
Not open for further replies.
  1. MerlinG2

    MerlinG2

    Joined:
    Jun 17, 2020
    Posts:
    44
    I'm trying to write a script that can randomly generate 'dungeons' but I have a problem where for lines 25, 31, 37, I get the error message NullReferenceException. I've added '// HERE' to where the problem is. Any help would be greatly appreciated!

    Here's the script:
    Code (CSharp):
    1. public int openingDirection;
    2.     private ComponentTemplate templates;
    3.     public bool spawned = false;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.         templates = GameObject.FindGameObjectWithTag("Components").GetComponent<ComponentTemplate>();
    9.         Invoke("Spawn", 0.1f);
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Spawn()
    14.     {
    15.         if (spawned == false)
    16.         {
    17.             if (openingDirection == 1)
    18.             {
    19.                 int rand = Random.Range(0, templates.bottomComponents.Length);
    20.                 Instantiate(templates.bottomComponents[rand], transform.position, templates.bottomComponents[rand].transform.rotation);
    21.                 Debug.Log("SpawnBottom");
    22.             }
    23.             else if (openingDirection == 2)
    24.             {
    25.                 int rand = Random.Range(0, templates.topComponents.Length); // HERE
    26.                 Instantiate(templates.topComponents[rand], transform.position, templates.topComponents[rand].transform.rotation);
    27.                 Debug.Log("SpawnTop");
    28.             }
    29.             else if (openingDirection == 3)
    30.             {
    31.                 int rand = Random.Range(0, templates.leftComponents.Length); // HERE
    32.                 Instantiate(templates.leftComponents[rand], transform.position, templates.leftComponents[rand].transform.rotation);
    33.                 Debug.Log("SpawnLeft");
    34.             }
    35.             else if (openingDirection == 4)
    36.             {
    37.                 int rand = Random.Range(0, templates.rightComponents.Length); // HERE
    38.                 Instantiate(templates.rightComponents[rand], transform.position, templates.rightComponents[rand].transform.rotation);
    39.                 Debug.Log("SpawnRight");
    40.             }
    41.             spawned = true;
    42.         }
    43.     }
     
  2. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    Hello,
    well it can't be much:
    GameObject.FindGameObjectWithTag("Components") is null
    or
    GameObject.FindGameObjectWithTag("Components").GetComponent<ComponentTemplate>() is null
    or
    templates.top/left/rightComponents is null
    or
    Length doesn't exist in templates.top/left/right.

    now it's up to you to know which one is true. I see you 'if' a lot so you're not exactly sure which 'openingDirection' is gonna work out, so checking null should be a good reflex anyway.
    If templates.bottomComponents.Length is never null, then it has something to do with your declarations of top, left and right OR that openingDirection is never equal to 1
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,758
    You have made it past step #1 below. Don't stop now!

    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.

    You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

    - drag it in using the inspector
    - code inside this script initializes it
    - some OTHER external code initializes it
    - ? something else?

    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
     
  4. MerlinG2

    MerlinG2

    Joined:
    Jun 17, 2020
    Posts:
    44
    Thanks! I figured out what it was an it was just a very simple issue. I had accidently applied the tag 'components' to the wrong game object.
     
  5. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    Most NullReferenceException are ;) Glad you could find the issue !
     
  6. abhikr1525

    abhikr1525

    Joined:
    Apr 25, 2023
    Posts:
    1
    Updating the Version Control package from your project resolves the bug.
    Project View -> Right-click Packages -> View in Package Manager -> Select Version Control -> Update
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    This is incorrect information. Also, please don't necro posts.
     
Thread Status:
Not open for further replies.