Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by AV_Corey, Oct 16, 2016.

  1. AV_Corey

    AV_Corey

    Joined:
    Jan 7, 2016
    Posts:
    122
    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 :)
     
    VarroPipes likes this.
  2. AV_Corey

    AV_Corey

    Joined:
    Jan 7, 2016
    Posts:
    122
    I found a work around for this which involves using an if statement for each dropdown value. For example:

    Code (CSharp):
    1. if (dropdown.value == 0)
    2. {
    3. amountToDeduct = 10;
    4. }
    5. if (dropdown.value == 1)
    6. {
    7. amountToDeduct = 25;
    8. }
    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.
     
  3. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    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:
    Code (CSharp):
    1. public DropDown coinsDropdown;
    2. // drag the dropdop object into this reference in the inspector
    3.  
    4. private readonly Dictionary<string, int> CoinDatabase = new Dictionary<string, int>()
    5. {
    6.     { "20 Coins", 20 },
    7.     { "30 Coins", 30 },
    8.     { "50 Coins", 50 }
    9. };
    10.  
    11. private void Awake()
    12. {
    13.     if (coinsDropdown != null)
    14.     {
    15.         coinsDropdown ClearOptions();
    16.         coinsDropdown.AddOptions(CoinDatabase.Keys.ToList());
    17.  
    18.         coinsDropdown.onValueChanged.AddListener(DropdownValueChanged);
    19.     }
    20. }
    21.  
    22. private void DropdownValueChanged(int newPosition)
    23. {
    24.     int realValue = CoinDatabase.Values.ElementAt(newPosition);
    25.     // realValue is the integer value associated with this key index
    26.     // do whatever you need to do with it here
    27. }
    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.
     
    Last edited: Oct 16, 2016
    BigRookGames, vivekhash and AV_Corey like this.
  4. AV_Corey

    AV_Corey

    Joined:
    Jan 7, 2016
    Posts:
    122
    Very helpful response, appreciate it :)
     
  5. No_Username_Found

    No_Username_Found

    Joined:
    Mar 25, 2014
    Posts:
    2
    Here is a simpler way (posted for future readers):
    Code (CSharp):
    1.  
    2.  
    3.                 //find your dropdown menu transform
    4.         public Transform dropdownMenu;
    5.  
    6.                 //find the selected index
    7.         int menuIndex = dropdownMenu.GetComponent<Dropdown> ().value;
    8.  
    9.                 //find all options available within the dropdown menu
    10.         List<Dropdown.OptionData> menuOptions = dropdownMenu.GetComponent<Dropdown> ().options;
    11.  
    12.                 //get the string value of the selected index
    13.         string value = menuOptions [menuIndex].text;
    14.  
     
    BigRookGames and cbaylissNTU like this.
  6. VarroPipes

    VarroPipes

    Joined:
    Jan 16, 2015
    Posts:
    2
    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.

    Code (CSharp):
    1. public GameObject dropdownLabel;
    2. Text dropdownText = dropdownLabel.GetComponent<Text>();
    3. string dropdownValue = dropdownText.text;

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

    Mistwraithe

    Joined:
    May 28, 2017
    Posts:
    4
    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...
     
  8. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    How to get dropdown value:

    How to get dropdown Text:
     
    mllhild likes this.
  9. ZoidbergForPresident

    ZoidbergForPresident

    Joined:
    Dec 15, 2015
    Posts:
    157
    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.
     
    bookicuvisuals and Kiihko like this.