0
\$\begingroup\$

I am trying to set a variable from the value of a DropDown menu in Unity.

I wrote a script as follows, attached it to the dropdown and set it to run it in Unity under: On Value Changed (Int32).

I have confirmed it is running when the dropdown changes from the debug output. However, it is giving me the NullReferenceException: Object reference not set to an instance of an object error when trying to get the dropdown's value.

The function I am running is ChangeLocationDropDown():

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

public class LocationSelectorDropDown : MonoBehaviour
{
public void ChangeLocationDropDown() {
        Debug.Log("DROP DOWN CHANGED");
        Dropdown m_Dropdown;
        m_Dropdown = GetComponent<Dropdown>();
        Debug.Log(m_Dropdown.options[m_Dropdown.value].text);
    }
}

I get the DROP DOWN CHANGED debug output but when I try to get the second debug it says:

NullReferenceException: Object reference not set to an instance of an object LocationSelectorDropDown.ChangeLocationDropDown ()

This Unity page suggests fixing this error by somehow using GameObject.Find to manually find the dropdown object first by its string name. But this is incredibly impractical and not a proper solution. I may need to make dozens of dropdowns. I need some simple clean way for each value to go into its own variable and/or update a database with their value with every change made.

What is the proper way to handle this?

ie. How do you update a string variable with the new text value of the DropDown?

\$\endgroup\$
1
  • \$\begingroup\$ Can you share screen shots of your scene hierarchy and the DropDown game object’s inspector? Your code should run if actually added as a component to your DropDown game object. If it’s a component of some other game object you’ll need to pass a reference to the DropDown component in your OnValueChanged delegate. \$\endgroup\$ Oct 11 at 9:22
2
\$\begingroup\$

I tried creating the same setup as you mentioned but I didn't got error. Seems there might be any small mistake, please check below. And also added the update to value to string variable part.


using UnityEngine;
using UnityEngine.UI;

public class LocationSelectorDropDown : MonoBehaviour
{
    [SerializeField]
    Text textBox;

    string valueText = "";

    public void ChangeLocationDropDown()
    {
        Debug.Log("DROP DOWN CHANGED");
        Dropdown m_Dropdown;
        m_Dropdown = GetComponent();
        Debug.Log(m_Dropdown.options[m_Dropdown.value].text);


        //store in variable
        valueText = m_Dropdown.options[m_Dropdown.value].text;
        //set textbox value
        textBox.text = valueText;
    }
}

Also please have a look at the output DropDownTest_Unity

P.S. : I am putting it as answer as for the moment i cannot comment

\$\endgroup\$
1
  • \$\begingroup\$ Thank you very much Hiren! You were right it worked. My problem was I was using a "Text Mesh Pro" dropdown and didn't realize the code only would work for a regular dropdown. For TMPro Dropdown I needed: TMP_Dropdown m_TMP_Dropdown; m_TMP_Dropdown = gameObject.GetComponent<TMP_Dropdown>(); Debug.Log(m_TMP_Dropdown.options[m_TMP_Dropdown.value].text); and that works. Thanks again \$\endgroup\$
    – mike
    Oct 14 at 6:18
2
\$\begingroup\$

Your code as presented will work, provided it is added as a component to a gameobject that also contains a Dropdown component, like so:

dropdown gameobject in inspector

However, if the script responding to the valueChange event is a component of some other game object, you'll need to provide a reference. Here's a simple example script:

using UnityEngine;
using UnityEngine.UI;

public class DDDemo : MonoBehaviour
{
    [SerializeField] private Dropdown m_Dropdown; // wire this in Inspector



   public void OnMyDropdownChanged()
    {
        Debug.Log(m_Dropdown.options[m_Dropdown.value].text);
    }
}

Which will work if wired up in this manner (here the script is attached to the Controller game object):

dropdown wiring controller wiring

\$\endgroup\$
1
  • \$\begingroup\$ Thank you very much Acme! You were right it worked. My problem was I was using a "Text Mesh Pro" dropdown and didn't realize the code only would work for a regular dropdown. For TMPro Dropdown I needed: TMP_Dropdown m_TMP_Dropdown; m_TMP_Dropdown = gameObject.GetComponent<TMP_Dropdown>(); Debug.Log(m_TMP_Dropdown.options[m_TMP_Dropdown.value].text); and that works. Thanks again \$\endgroup\$
    – mike
    Oct 14 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.