Accessing the value of an option in a dropdown (UI)

So I need to deduct the value of an option that a user chooses from a dropdown list from an int.

For example if the user has 50 coins, selects the ‘10’ option from the dropdown list and presses a button they end up with 40 coins.

If someone could point me in the right direction it would be much appreciated :slight_smile:

1 Like

I found a work around for this which involves using an if statement for each dropdown value. For example:

if (dropdown.value == 0)
{
amountToDeduct = 10;
}
if (dropdown.value == 1)
{
amountToDeduct = 25;
}

I am still curious to find out if the value of an option in a dropdown list can still be accessed though as I think it would be a more professional way of going about things.

The dropdown doesn’t store values, it’s just a list of string objects and the “value” is actually only the position of that object within the list. The way that you’ve done it is the simplest solution, though you should use “else if” instead so that it’s not pointlessly checking over and over.

Other ways that you can manage this really depend on your specific circumstances. For instance, let’s say you create a dictionary that holds a keyvaluepair of “string” and “integer” types. The string is the text that you want to display in the dropdown options, while the integer is the real value you want that option to represent.

Now, in order to populate the dropdown dynamically, we’ll do something like this:

public DropDown coinsDropdown;
// drag the dropdop object into this reference in the inspector

private readonly Dictionary<string, int> CoinDatabase = new Dictionary<string, int>()
{
    { "20 Coins", 20 },
    { "30 Coins", 30 },
    { "50 Coins", 50 }
};

private void Awake()
{
    if (coinsDropdown != null)
    {
        coinsDropdown ClearOptions();
        coinsDropdown.AddOptions(CoinDatabase.Keys.ToList());

        coinsDropdown.onValueChanged.AddListener(DropdownValueChanged);
    }
}

private void DropdownValueChanged(int newPosition)
{
    int realValue = CoinDatabase.Values.ElementAt(newPosition);
    // realValue is the integer value associated with this key index
    // do whatever you need to do with it here
}

That’ll programmatically set the dropdown’s options, clearing whatever was set before, putting them in the order that we’ve added them in the dictionary, assigning the proper event handler function for when the user changes the current selection, and then using the positions in the collection to determine the “real” value of the item selected and doing something with that. The “ToList” and “.ElementAt” extension methods are a part of the System.Linq namespace, so be sure to include that.

This isn’t really a totally safe setup, as any re-ordering of the dictionary will scramble the results, so a safer bet would be to use a readonly style array of keyvaluepairs and doing the lookup with Linq or something. This is only meant as an example, but as long as you don’t reorder the dictionary it should be fine I think.

3 Likes

Very helpful response, appreciate it :slight_smile:

Here is a simpler way (posted for future readers):

                //find your dropdown menu transform
        public Transform dropdownMenu;

                //find the selected index
        int menuIndex = dropdownMenu.GetComponent<Dropdown> ().value;

                //find all options available within the dropdown menu
        List<Dropdown.OptionData> menuOptions = dropdownMenu.GetComponent<Dropdown> ().options;

                //get the string value of the selected index
        string value = menuOptions [menuIndex].text;
3 Likes

A better way is to grab the “Label” child object of the dropdown instead, and get the text value from there. The Unity dropdown object will store the text value of the selection in this label.

Running in debug mode in the inspector will show this value.

public GameObject dropdownLabel;
Text dropdownText = dropdownLabel.GetComponent<Text>();
string dropdownValue = dropdownText.text;

Unity can have some REALLY bad documentation for game objects sometimes.

1 Like

Which doesn’t explain why the dropdown doesn’t have the ability to have separate display text and key values for each item in the dropdown. Surely this has been standard functionality for business development software for maybe a decade now? Games have similar requirements, I don’t know why they are going through the same process of slowly rediscovering the wheel…

2 Likes

How to get dropdown value:

How to get dropdown Text:

1 Like

What if I want to get a real “value” from the dropdown selection, like a class instance I’ve written?

It’s basically loading decks of cards that have been loaded from files. I want to get the selected deck in the list and load it appropriately in memory.

2 Likes