NullReferenceException | Object reference not set to an instance of an object.

Hey,

I have an issue in my Resolution Settings.

Here is the Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.IO;
using TMPro;
using UnityEngine.UI;

public class Settings_Manager : MonoBehaviour{

    #region Attributes

    [SerializeField]
    private Text resolutionText;

    private Resolution[] resolutions;
    private int currentResolutionIndex = 0;

    private const string RESOLUTION_PREF_KEY = "Resolution";

    #endregion

    void Start(){

        Resolution[] resolutions = Screen.resolutions;

        currentResolutionIndex = PlayerPrefs.GetInt(RESOLUTION_PREF_KEY, 0);

       resolutionText.text = Screen.currentResolution.width + "x" + Screen.currentResolution.height + "@" + Screen.currentResolution.refreshRate;
    }

    #region Resolution
    private void SetResolutionText(Resolution resolution){
        resolutionText.text = resolution.width + "x" + resolution.height + "@" + resolution.refreshRate;
    }
    public void SetNextResolution(){
        currentResolutionIndex = GetNextWrappedIndex(resolutions, currentResolutionIndex);
        SetResolutionText(resolutions[currentResolutionIndex]);
    }

    public void SetPreviousResolution()
    {
        currentResolutionIndex = GetPreviousWrappedIndex(resolutions, currentResolutionIndex);
        SetResolutionText(resolutions[currentResolutionIndex]);
    }

    private int GetNextWrappedIndex<T>(IList<T> collection, int currentIndex){
        if (collection.Count < 1) return 0;
        return (currentIndex + 1) % collection.Count;
    }

    private int GetPreviousWrappedIndex<T>(IList<T> collection, int currentIndex){
        if (collection.Count < 1) return 0;
        if ((currentIndex - 1) < 0) return collection.Count - 1;
        return (currentIndex - 1) % collection.Count;
    }

    private void SetAndApplyResolution(int newResolutionIndex){
        currentResolutionIndex = newResolutionIndex;
        ApplyCurrentResolution();
    }

    private void ApplyCurrentResolution(){
        ApplyResolution(resolutions[currentResolutionIndex]);
    }

    private void ApplyResolution(Resolution resolution){
        SetResolutionText(resolution);

        Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen, Screen.currentResolution.refreshRate);
        PlayerPrefs.SetInt(RESOLUTION_PREF_KEY, currentResolutionIndex);
    }

    #endregion

    public void ApplyChanges(){
        SetAndApplyResolution(currentResolutionIndex);
    }
}

An the 3 Errors

NullReferenceException: Object reference not set to an instance of an object
Settings_Manager.GetNextWrappedIndex[T] (System.Collections.Generic.IList`1[T] collection, System.Int32 currentIndex)
NullReferenceException: Object reference not set to an instance of an object
Settings_Manager.GetNextWrappedIndex[T] (System.Collections.Generic.IList`1[T] collection, System.Int32 currentIndex)
NullReferenceException: Object reference not set to an instance of an object
Settings_Manager.ApplyCurrentResolution ()

You did not post the line on which the error occurs, so all i can tell you is that something in your ApplyCurrentResolution() method is null. A “NullReferenceException” basically means that you try to access something in an undefined object reference. For example, let’s say you have a class called Character, and this class has a public field for its hpValue. If you declare a “Character hero;” and then try to access “hero.hpValue”, then it will throw a NullReferenceException, since the object reference hero was never assigned, thus is null.

In your case this means that some object in the ApplyCurrentResolution, or the methods called in there, is null. So i would start by checking involved variables, like “resolutions” and the entry you send to ApplyResolution() for null.

1 Like

Thanks, i found the error with your tip.

Glad to hear that!
But it would be nice if you posted your solution for people who stumble upon this post in the future, so they can see what went wront for you and thus may get a better idea for how to fix their own problem :slight_smile: