Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Null Reference Exception even though I assigned it in the editor

Discussion in 'Scripting' started by EpicGameDev1, Jun 16, 2021.

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

    EpicGameDev1

    Joined:
    Apr 14, 2021
    Posts:
    20
    So I have a script, and I get a null reference error when I load the game even though I assigned the variable in the editor. Here's the code and a screenshot:
    Code (CSharp):
    1. public TMP_Dropdown resolutionDropdown;
    2. Resolutions[] resolutions;
    3. void Start()
    4. {
    5.     resolutions = Screen.resolutions;
    6.  
    7.         resolutionDropdown.ClearOptions(); //Null Reference Error is here
    8.  
    9.         List<string> options = new List<string>();
    10.  
    11.         for (int i = 0; i < resolutions.Length; i++)
    12.         {
    13.             string option = resolutions[i].width + " x " + resolutions[i].height;
    14.             options.Add(option);
    15.         }
    16.  
    17.         resolutionDropdown.AddOptions(options); //Null Reference Error is also here
    18. }
    the script still works though like the function still works. also when i did
    Code (CSharp):
    1. resolutionDropdown = GameObject.Find("Resolution Dropdown").GetComponent<TMP_Dropdown>();
    it didn't give me the error.

    also i remember using this exact same script and not getting this error.
     

    Attached Files:

    Last edited: Jun 16, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,006
    All of the standard approaches apply. Nothing unusual here, just a null ref and you haven't finished step 2 yet.

    Three steps to solve them all:

    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

    Don't forget the possibility you dragged this onto ANOTHER GameObject and didn't fill that one out.
     
    sonictimm likes this.
  3. EpicGameDev1

    EpicGameDev1

    Joined:
    Apr 14, 2021
    Posts:
    20
    thank you i found my answer on the last line that you spoke and then i remembered that i put the script onto another object thank you
     
    sonictimm and Kurt-Dekker like this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,006
    I've been fixing null references for over 40 years and the three-step approach has yet to fail! :)
     
  5. somethingschanged

    somethingschanged

    Joined:
    Jun 21, 2020
    Posts:
    2
    Although there was no problem in the first game, when I reload the scene, null ref errors appear, I don't understand why :(
    upload_2022-10-13_0-55-47.png
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,006
    You will continue to never understand why until you start with step #1 of the three step process to fix it.

    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 <------ actions taken before this step are ALWAYS a waste of your time.
    - Identify why it is null
    - Fix that

    You'll notice that "necro-posting to a 2021 thread" is NOT listed in the above three steps. :)
     
  7. Bobane

    Bobane

    Joined:
    May 13, 2014
    Posts:
    17
    The error here is a different one, its an engine problem. It appears here as well
    https://discussions.unity.com/t/ima...-when-i-assign-it-myself-in-the-editor/240279

    and I am getting it as well. The first time you run the project everything is fine. Anything after the first run returns a null, even though the object is referenced, by hand non-the-less. Im a professional game dev for reference
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,006
    Considering he posted no code I would be FAR more inclined to think he got his boolean logic backward.

    Such as:

    Code (csharp):
    1. if (imageComponent == null) print( "good"); else print( "null");
    You will know it might be an engine problem with the source of the exception isn't a line of code in your script.
     
  9. AVTHS

    AVTHS

    Joined:
    Dec 6, 2023
    Posts:
    1
    No, i can confirm this is a problem, i experience it as well. Added a reference to a image in the inspector, assigned it and yet its null when running. No code interfering with this component.

    upload_2024-3-19_16-5-25.png upload_2024-3-19_16-6-2.png
     

    Attached Files:

  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,576
    This thread is not related to your problem. Getting a NullReferenceException doesn't have a single root cause whether it be something unassigned, a bug in Unity or your code.

    Please create your own threads and ask you own questions rather than necro/hijack existing ones.

    Thanks.
     
    Kurt-Dekker likes this.
Thread Status:
Not open for further replies.