Why do I get this Error in the Console?


I followed this tutorial on Youtube and actually it works for me too. but I still get this error message immediately after starting the editor.
At the moment this message has no influence on my desired function, but I would rather have the error message completely gone in case there could be trouble later when compiling etc.
Why does the error appear to me?
Because I followed the tutorial pretty closely and I drag and droped the Dropdown instance from my hierarchy into the “public Dropdown DropDownVar;”:

here is my code that makes this error:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SettingsMenu : MonoBehaviour
{
    // ==================== Resolution Dropdown ====================

    public Dropdown DropDownVar;

    private void Start()
    {

        DropDownVar.onValueChanged.AddListener(delegate
        {
            DropDownSettingsValueWechselPassiert(DropDownVar);
        });

    }

    //Beim wechsel eines Wertes im Dropdown, wird das hier ausgefuehrt:
    public void DropDownSettingsValueWechselPassiert(Dropdown transmitter)
    {
        switch(transmitter.value)
        {
            case 0:
                // Switch to 640 x 480 full-screen at 60 hz
                Screen.SetResolution(Display.main.systemWidth, Display.main.systemHeight, true, 60);
                break;
            case 1:
                // Switch to 640 x 480 in full-screen
                Screen.SetResolution(480, 640, true);
                break;
            case 2:
                Screen.SetResolution(720, 1280, true);
                break;
            case 3:
                Screen.SetResolution(1080, 1920, true);
                break;
            case 4:
                Screen.SetResolution(1440, 2560, true);
                break;
            case 5:
                Screen.SetResolution(2160, 3840, true);
                break;
            case 6:
                Screen.SetResolution(2160, 4096, true);
                break;


        }
    }
}

You have not assigned the “DropDownVar” variable on this script. If you are not noticing any affect on the function of your game, the most likely issue is that you have a second copy of the “SettingsMenu” script floating around in your scene somewhere by accident, and on this second copy, you have not assigned the variable. Otherwise this error would definitely break the functionality of this script.

2 Likes

It looks like your “DropDownVar” is null. Put a “Debug.Log(DropDownVar);” in line 13 and check the console output. Does the dropdown work if you “play” it ?

Thank you for this quick and extremely helpful answer!
I have now made a new script especially for this dropdown. Because I intentionally used the current script for other objects before, because the script was originally supposed to contain all setting options like audio sliders etc…