UI > new Dropdown element... how to use template options to send their index?

I am playing around with the new useful looking Dropdown element in the unity UI.

It has a field to enter Options, from this is auto generates a list of Item objects at runtime.

These Item objects are derrived from a template, that is hidden when you create a Dropdown from the the new UI elements in Unity 5.2

At run time, Unity will Instantiate the template by the amount of Options you have added in the Dropdown Inspector.

Using the event system and the inspector, how can I grab the option that has been selected at runtime? The options are automatically deleted when the user selects an option in the dropdown, as per the functionality of this new UI element.

In the docs it says this can be done, by sending the Int of the option selected through the On Value Changed system, but it’s not entirely obvious in the inspector using the On Value Changed. As this can only be applied to the template Item or the Dropdown, and neither of them record which option index was clicked, only the base Int you have when assigning a void to the slot. ( which is 0).

Docs: Redirecting to latest version of com.unity.ugui

The Dropdown inspector records the option clicked, as “Value”. Although I am not sure how to access this through code.

Any advice on how to use this new UI element would be greatly appreciated.

Fairly simple. You simply need a function in a script attached to a GO that takes an Int parameter, for example:

public class SelectedCombo : MonoBehaviour {
    // Use this for initialization
    public void ShowResult (int result) {
        Debug.Log ("My Result is: " + result);;
    }
}

Then in the “onValueChanged” event for the dropdown, select the GO with the script and pass the “dynamic” value from the event, as shown here:
2350320--159137--upload_2015-10-22_17-8-38.png

This gives you the index of the selected combobox item. (the receiving function has to have an INT parameter for the dynamic option to appear)

Perfect! This works a treat.

Thanks for the help.

Hello

I’m new on unity environment, but am having problem to understand Dropdown. I did add options on the dropdown inspector but when am running my game then i press on the arrow to allow me to view other options instead am having this error:
The dropdown template is not valid. The Item Text must be on the item GameObject or children of it.
UnityEngine.EventSystems.EventSystem:Update().

And i would like to have a nice direction or general knowledge on unity, am using unity 5.3

Thanks

Don’t mind i fix. I delete it then i add new dropdown it working perfectly.