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

Object reference not set to an instance of an object

Discussion in 'Scripting' started by RogueEclips3, Feb 7, 2020.

  1. RogueEclips3

    RogueEclips3

    Joined:
    Feb 6, 2020
    Posts:
    4
    I was making some menus for my game with some help from a couple of tutorials. I finished and tested them, but noticed an error in the settings menu script. The error is in the SetResolution method, but I don't get why it's giving me an error. Everything works perfectly fine, but the error still comes up, which confuses me. This is the error and my code. Can anyone please help me?

    NullReferenceException: Object reference not set to an instance of an object
    SettingsMenu.SetResolution (System.Int32 resolutionIndex) (at Assets/SettingsMenu.cs:48)

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.Audio;
    4. using UnityEngine.UI;
    5.  
    6. public class SettingsMenu : MonoBehaviour
    7. {
    8.  
    9.     public AudioMixer audioMixer;
    10.  
    11.     public Dropdown resolutionDropdown;
    12.  
    13.     public Dropdown graphicsDropdown;
    14.  
    15.     Resolution[] resolutions;
    16.  
    17.     void Start()
    18.     {
    19.         resolutions = Screen.resolutions;
    20.  
    21.         resolutionDropdown.ClearOptions();
    22.  
    23.         List<string> options = new List<string>();
    24.  
    25.         int currentResolutionIndex = 0;
    26.         for (int i = 0; i < resolutions.Length; i++)
    27.         {
    28.             string option = resolutions[i].width + " x " + resolutions[i].height;
    29.             options.Add(option);
    30.  
    31.             if (resolutions[i].width == Screen.currentResolution.width && resolutions[i].height == Screen.currentResolution.height)
    32.             {
    33.                 currentResolutionIndex = i;
    34.             }
    35.         }
    36.  
    37.         resolutionDropdown.AddOptions(options);
    38.         resolutionDropdown.value = currentResolutionIndex;
    39.         resolutionDropdown.RefreshShownValue();
    40.  
    41.         int currentGraphicsIndex = QualitySettings.GetQualityLevel();
    42.  
    43.         graphicsDropdown.value = currentGraphicsIndex;
    44.     }
    45.  
    46.     public void SetResolution(int resolutionIndex)
    47.     {
    48.         Resolution resolution = resolutions[resolutionIndex];
    49.         Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen);
    50.     }
    51.  
    52.     public void SetVolume(float volume)
    53.     {
    54.         audioMixer.SetFloat("masterVolume", volume);
    55.     }
    56.  
    57.     public void SetQuality(int qualityIndex)
    58.     {
    59.         QualitySettings.SetQualityLevel(qualityIndex);
    60.     }
    61.  
    62.     public void SetFullscreen(bool isFullscreen)
    63.     {
    64.         Screen.fullScreen = isFullscreen;
    65.     }
    66. }
    67.  
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    The only way I see to get that error on line 48 is if the variable "resolutions" doesn't have a value. Since you are assigning it a value in the Start() function on line 19, this suggests either that the SetResolution function is being called before Start(), or something else is clearing the variable's value at a later time. If the error only happens once each time you run your program and after that your code appears to work, then the former is more likely.

    Calling SetResolution before Start might happen if the game object is initially inactive, or if another script tries to call it from Awake or Start. (If you look at the error message in more detail, it might tell you where it's being called from.)
     
  3. RogueEclips3

    RogueEclips3

    Joined:
    Feb 6, 2020
    Posts:
    4
    Just going to clarify here, I am new to C# coding.
    The error only shows up at the very start of the simulation and from then on it works perfectly fine.

    I watched a tutorial to do this. It's the exact same code, but the person who did the tutorial did not come up with any errors. This is probably because the tutorial was made in late 2017. I just needed to clarify that as well.

    EDIT: The game object is, in fact, initially inactive. Now I just need to find a way to make SetResolution trigger after Start.

    EDIT 2: I found a solution. I have the game object active initially and in the method Start() I deactivate it. This prevents SetResolution from triggering before the Start() method.
     
    Last edited: Feb 7, 2020
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    You haven't shown the thing that's currently calling SetResolution.