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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Bug Why is there a NullReferenceException when there is no object being referenced?

Discussion in 'Scripting' started by Richard_Ingalls, Dec 24, 2022.

  1. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    Code (CSharp):
    1. public GameObject LevelUpUI;
    2.     delegate void methodGrabber();
    3.     List<(string Name, methodGrabber Value)> methods1;
    4.     int index1;
    5.     public TMP_Text A1;
    6.  
    7. void Start()
    8.     {
    9.         methods1 = new List<(string Name, methodGrabber Value)>();
    10.         methods1.Add((nameof(ProjectileSizeUp), ProjectileSizeUp));
    11.         methods1.Add((nameof(ProjectileSpeedUp), ProjectileSpeedUp));
    12.         methods1.Add((nameof(ProjectileNumberUp), ProjectileNumberUp));
    13.     }
    14.  
    15. void OnEnable()
    16.     {
    17.         method1();
    18.     }
    19.  
    20. void method1()
    21.     {
    22.         index1 = Random.Range(0, methods1.Count + 1);//this is where the error occurs
    23.         A1.text = methods1[index1].Name;
    24.     }
    25.  
    26. public void ButtonFunc0()
    27.     {
    28.         methods1[index1].Value();
    29.         LevelUpUI.SetActive(false);
    30.         Time.timeScale = 1;
    31.     }
    This is a condensed version of my code. It occurs nowhere else, even though I have multiple of these functions. Does anyone know why this could be?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,148
    It's always helpful to include the error as it shows a line number. Granted, with a condensed version of your code, it wouldn't really match up, so you'd want to include what line the error is pointing to.

    But for references. Both your LevelUpUI and A1 variables are references. So it's possible either could be null.
     
  3. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,799
    Try Methods.Count - 1 to stay within the index range.
     
    Ryiah likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,945
    You are referencing MANY different things above that could be null.

    Nope, but only YOU can find out. It's ALWAYS the same three steps, ALWAYS.

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that

    For one, the OnEnable is going to run LONG before Start() runs.
     
    Bunny83 likes this.
  5. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    I included it, here in the condensed code it is line 22, where I put a comment.
     
  6. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    The thing is, it was working before, but I changed a few things so that the behavior would be as I wanted. And thanks for the information about OnEnable, I had just assumed that it would run after start does, that's likely where the issue is popping up. I'll get back to you after I've tried changing Start to Awake.
     
    Last edited: Dec 24, 2022
  7. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    Well, it seems to have worked. Thanks for you're help!
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,945
    This is definitely a diagram that I cannot live without:

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
  9. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,903
    In addition, your use of Random will fail if you're using UnityEngine.Random.Range() like that.

    The Integer form of UnityEngine.Random.Range() is inclusive on the bottom exclusive on the top. It's designed that way so you don't have to add one to pick a random element. If you do add one, you'll eventually pick an index outside the range of your array.
     
    Bunny83, Ryiah, Yoreki and 1 other person like this.
  10. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    OnEnable is called before OnStart so your methods1 list hasn't been created yet.
     
    Bunny83 likes this.