0

I am following from this tutorial https://www.youtube.com/watch?v=YOaYQrN1oYQ and when I am trying to create the resolutions dropdown, there are multiple errors, they are

NullReferenceException: Object reference not set to an instance of an object
UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor[] editors, Int32 editorIndex, Boolean rebuildOptimizedGUIBlock, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect) (at /Users/builduser/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1114)
UnityEditor.InspectorWindow.DrawEditors (UnityEditor.Editor[] editors) (at /Users/builduser/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1073)
UnityEditor.InspectorWindow.OnGUI () (at /Users/builduser/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:413)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115

I have set the public resolution dropdown to something, and the error is not to do with above the line that states

public void SetVolume (float volume)

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;

public class scr : MonoBehaviour {

    public AudioMixer audioMixer;

    public Dropdown resolutionDropdown;

    Resolution[] resolutions;

    void Start ()
    {
        resolutions =  Screen.resolutions;

        resolutionDropdown.ClearOptions();

        List<string> options = new List<string>();

        for (int i = 0; i < resolutions.Length; i++)
        {
            string option = resolutions[i].width + " x " + resolutions[i].height;
            options.Add(option);

            if (resolutions[i].width == Screen.currentResolution.width && 
                resolutions[i].height == Screen.currentResolution.height)

            {
                currentResolutionIndex = i;
            }
        }

        resolutionDropdown.AddOptions(options);
        resolutionDropdown.value = currentResolutionIndex;
        resolutionDropdown.RefreshShownValue();

    }
    public void SetResolution(int resolutionIndex)
    {
    Screen.SetResolution(resolution.width, resolution.height, Screen.fullscreen);
    }
    public void SetVolume (float volume)
    {
        audioMixer.SetFloat("volume", volume);
    }

    public void SetQuality (int qualityIndex)
    {
        QualitySettings.SetQualityLevel(qualityIndex);
    }

    public void SetFullscreen (bool isFullscreen)
    {
        Screen.fullScreen = isFullscreen;
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
mohammed
  • 1
  • 5
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Neijwiert Apr 12 '18 at 09:07
  • Nope wasnt a duplicate, the answer was below – mohammed Apr 12 '18 at 09:50

1 Answers1

1

Your stacktrace indicates an exception thrown from the editor and not your scripts. Try restarting Unity.

From Unity Answers.

Ian H.
  • 3,840
  • 4
  • 30
  • 60
Edwin Chua
  • 658
  • 1
  • 8
  • 20
  • Well that was the solution, but thanks so much this was so confusing for me – mohammed Apr 12 '18 at 09:49
  • After that there were a few errors but were easy to fix, the resolution.width was screen.width and also with resolution.height, and i forgot to make a variable for currentResolutionIndex, anyways thanks :D – mohammed Apr 12 '18 at 09:56
  • Glad I could help :) please mark as answered. Thank you – Edwin Chua Apr 12 '18 at 10:06