"Object reference not set to an instance of an object"

Well, probably the most common NullReferenceException for C# coders so far :slight_smile:

I was following GameGrind’s tutorial on creating a settings menu for my game.
I made it, I wrote the Scripts, and so they look:

That’s the class storing settings data

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public  class GameSettings
{
    public  bool ScreenResOn;
    public  int ResolutionIndex;
    public  int TextureQualityIndex;
    public  int AntialiasingIndex;
    public  float MouseSensitivity;
    public  float PlayerSpeed;
}

This is the script attached to my game settings menu

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;                                            //For saving external files

public class SettingsManager : MonoBehaviour {

    //**Referebce to all option's gameobjects
    public Toggle FullscreenToggle;
    public Dropdown ResolutionDropdown;
    public Dropdown TextureQualityDropdown;
    public Dropdown AntialiasingDropdown;
    public Slider MouseSensitivitySlider;
    public Slider PlayerSpeedSlider;
    public Button ApplyButton;

    public GameSettings MySettings;        //Create an istance of GameSettings class to save/retreive data
                                           
    public Resolution[] ResolutionList;        //Array containing available resolutions on ThisPC

    // Use this for initialization
    void OnEnable ()
    {
        //Series of methods that delegate the default "OnValue" methods to custom ones
        FullscreenToggle.onValueChanged.AddListener(delegate { OnFullscreenToggle(); });
        ResolutionDropdown.onValueChanged.AddListener(delegate { OnResolutionChange(); });
        TextureQualityDropdown.onValueChanged.AddListener(delegate { OnTextureQualityChange(); });
        AntialiasingDropdown.onValueChanged.AddListener(delegate { OnAntialiasingChange(); });
        MouseSensitivitySlider.onValueChanged.AddListener(delegate { OnMouseSensitivityChange(); });
        PlayerSpeedSlider.onValueChanged.AddListener(delegate { OnPlayerSpeedChange(); });
        ApplyButton.onClick.AddListener (delegate {    SaveSettings ();});

        ResolutionList = Screen.resolutions;                    //Get available res

        //Fill the Res dropdown list
        foreach (Resolution res in ResolutionList)
        {
            ResolutionDropdown.options.Add (new Dropdown.OptionData (res.ToString ()));
        }

        LoadSettings ();
    }
   
    //Set settings and store data into MySettings class
    public void OnFullscreenToggle()
    {
        Screen.fullScreen = MySettings.ScreenResOn = FullscreenToggle.isOn;
    }

    public void OnResolutionChange()
    {
        Screen.SetResolution (ResolutionList [ResolutionDropdown.value].width, ResolutionList [ResolutionDropdown.value].height, Screen.fullScreen);
        MySettings.ResolutionIndex = ResolutionDropdown.value;
    }

    public void OnTextureQualityChange()
    {
        QualitySettings.masterTextureLimit=MySettings.TextureQualityIndex = TextureQualityDropdown.value;

    }

    public void OnAntialiasingChange()
    {
        QualitySettings.antiAliasing =    MySettings.AntialiasingIndex = (int)Mathf.Pow (2f, AntialiasingDropdown.value);
           
    }
    public void OnMouseSensitivityChange()
    {
        MySettings.MouseSensitivity = MouseSensitivitySlider.value;
    }

    public void OnPlayerSpeedChange()
    {
        MySettings.PlayerSpeed = PlayerSpeedSlider.value;
    }

    public void SaveSettings()
    {
        string myData = JsonUtility.ToJson (MySettings, true);        //Save public fields into
        File.WriteAllText (Application.persistentDataPath + "/gamesettings.json", myData);
        Debug.Log (Application.persistentDataPath);
    }

    public void LoadSettings()
    {
        MySettings = JsonUtility.FromJson<GameSettings> (File.ReadAllText(Application.persistentDataPath + "/gamesettings.json"));
        FullscreenToggle.isOn = MySettings.ScreenResOn;
        ResolutionDropdown.value = MySettings.ResolutionIndex;
        TextureQualityDropdown.value = MySettings.TextureQualityIndex;
        AntialiasingDropdown.value = MySettings.AntialiasingIndex;
        PlayerSpeedSlider.value = MySettings.PlayerSpeed;
        MouseSensitivitySlider.value = MySettings.MouseSensitivity;

        ResolutionDropdown.RefreshShownValue ();

        //Load real settings
        Screen.fullScreen=FullscreenToggle.isOn;
        Screen.SetResolution (ResolutionList [ResolutionDropdown.value].width, ResolutionList [ResolutionDropdown.value].height, Screen.fullScreen);
        QualitySettings.masterTextureLimit = TextureQualityDropdown.value;
        QualitySettings.antiAliasing=(int)Mathf.Pow (2f, AntialiasingDropdown.value);
    }

}

And this is the visual layout:

Everytime I try to change any value of the dropdown, slider or toggle it notices that I haven’t set an object reference.

After many researches, the only problem I found is the following:
The Json file “gamesettings” is empty, why can it happen that?
The path is correct and the fields are public.

Thank you for anyone helping :slight_smile:

Are all the Sliders/Dropdowns/Toggles dragged into the appropriate fields in the inspector?

Take a look at their fromJson example. Your GameSettings is probably just needing the System.Serializable

1 Like

Oh, yes, thanks. How can I make my class Serialized?

It…shows you in the link…Just look where they added the System.Serialiable.

I don’t know why last time I didn’t get it working, but now it’s all fine. Thank you very much
I added [System.Serialize] in GameSettings.cs before
public class GameSettings