Search Unity

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

Discussion in 'Scripting' started by sullaysur, Oct 20, 2019.

  1. sullaysur

    sullaysur

    Joined:
    Dec 26, 2018
    Posts:
    8
    Hey,

    I have an issue in my Resolution Settings.

    Here is the Code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using System.IO;
    6. using TMPro;
    7. using UnityEngine.UI;
    8.  
    9. public class Settings_Manager : MonoBehaviour{
    10.  
    11.     #region Attributes
    12.  
    13.     [SerializeField]
    14.     private Text resolutionText;
    15.  
    16.     private Resolution[] resolutions;
    17.     private int currentResolutionIndex = 0;
    18.  
    19.     private const string RESOLUTION_PREF_KEY = "Resolution";
    20.  
    21.     #endregion
    22.  
    23.     void Start(){
    24.  
    25.         Resolution[] resolutions = Screen.resolutions;
    26.  
    27.         currentResolutionIndex = PlayerPrefs.GetInt(RESOLUTION_PREF_KEY, 0);
    28.  
    29.        resolutionText.text = Screen.currentResolution.width + "x" + Screen.currentResolution.height + "@" + Screen.currentResolution.refreshRate;
    30.     }
    31.  
    32.     #region Resolution
    33.     private void SetResolutionText(Resolution resolution){
    34.         resolutionText.text = resolution.width + "x" + resolution.height + "@" + resolution.refreshRate;
    35.     }
    36.     public void SetNextResolution(){
    37.         currentResolutionIndex = GetNextWrappedIndex(resolutions, currentResolutionIndex);
    38.         SetResolutionText(resolutions[currentResolutionIndex]);
    39.     }
    40.  
    41.     public void SetPreviousResolution()
    42.     {
    43.         currentResolutionIndex = GetPreviousWrappedIndex(resolutions, currentResolutionIndex);
    44.         SetResolutionText(resolutions[currentResolutionIndex]);
    45.     }
    46.  
    47.     private int GetNextWrappedIndex<T>(IList<T> collection, int currentIndex){
    48.         if (collection.Count < 1) return 0;
    49.         return (currentIndex + 1) % collection.Count;
    50.     }
    51.  
    52.     private int GetPreviousWrappedIndex<T>(IList<T> collection, int currentIndex){
    53.         if (collection.Count < 1) return 0;
    54.         if ((currentIndex - 1) < 0) return collection.Count - 1;
    55.         return (currentIndex - 1) % collection.Count;
    56.     }
    57.  
    58.     private void SetAndApplyResolution(int newResolutionIndex){
    59.         currentResolutionIndex = newResolutionIndex;
    60.         ApplyCurrentResolution();
    61.     }
    62.  
    63.     private void ApplyCurrentResolution(){
    64.         ApplyResolution(resolutions[currentResolutionIndex]);
    65.     }
    66.  
    67.     private void ApplyResolution(Resolution resolution){
    68.         SetResolutionText(resolution);
    69.  
    70.         Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen, Screen.currentResolution.refreshRate);
    71.         PlayerPrefs.SetInt(RESOLUTION_PREF_KEY, currentResolutionIndex);
    72.     }
    73.  
    74.     #endregion
    75.  
    76.     public void ApplyChanges(){
    77.         SetAndApplyResolution(currentResolutionIndex);
    78.     }
    79. }
    80.  
    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 ()
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    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.
     
    Last edited: Oct 20, 2019
    sullaysur likes this.
  3. sullaysur

    sullaysur

    Joined:
    Dec 26, 2018
    Posts:
    8
    Thanks, i found the error with your tip.
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    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 :)