How do you access the text value of the Dropdown UI?
Whenever the value is changed, I need to get the text as string and print it out. I have tried using the code below but it always returns null. Thank you in advance.
var LocationPicker : GameObject;
var LocPickerString : String;
function Start () {
LocPickerString = LocationPicker.GetComponent.<UI.Dropdown>().OptionData().text;
Debug.Log(LocPickerString);}
Hello !
LocationPickerLabel.GetComponent<Dropdown>().GetComponentInChildren<Text>().text
:D
Thanks, that may work, too. I haven't tried it, though.
In a 4 $$anonymous$$utes tutorial you can learn it easily https://www.youtube.com/watch?v=FteXwEdED0Y&t=2s
How to get dropdown value https://www.youtube.com/watch?v=FteXwEdED0Y
Answer by rsouza · Jun 15, 2017 at 03:10 PM
You can simply do this:
Debug.Log(Dropdown.options[Dropdown.value].text);
The logic behind this is solid. It's extremely simple and works like a charm. Thank you.
Answer by No_Username_Found · Mar 05, 2017 at 05:24 AM
I couldn't get the above answers to work, so here is another (simpler) option for future readers:
//find your dropdown object
public Transform dropdownMenu;
//get the selected index
int menuIndex = dropdownMenu.GetComponent<Dropdown> ().value;
//get all options available within this dropdown menu
List<Dropdown.OptionData> menuOptions = dropdownMenu.GetComponent<Dropdown> ().options;
//get the string value of the selected index
string value = menuOptions [menuIndex].text;
Unless for temporary use, it is better to work with the index. Problem will arise when globalizing your text!
Answer by AlvinHerawan · Apr 08, 2016 at 10:43 PM
Turns out that the selected value is always reflected real time in the Text component of a child of the Dropdown, which is the Label. So here is what I had done:
var LocationPickerLabel : GameObject;
private var SelectedLocation : Text;
var LocName : String;
function Start () {
SelectedLocation = LocationPickerLabel.GetComponent(UI.Text);
LocName = SelectedLocation.text;}
A little out of topic, but I can also set the value from the start and therefore typing in an instruction before the drop down is pressed by typing SelectedLocation.text = "Any instruction that I need ..." in the start function.
Doesn't work when you manually override the label. In my case it says "Please select your instrument", but Piano is selected by default.
It's been a while since I worked with this issue. Your case certainly needs better solution than this. Perhaps the other answers below could help you get some ideas on how to proceed with your case?
Answer by QuintonH · Apr 08, 2016 at 10:12 PM
Try: LocPickerString = LocationPicker.GetComponent.<UI.Dropdown>().itemText.text
Dropdown Documentation: http://docs.unity3d.com/ScriptReference/UI.Dropdown.html
Thanks for the quick reply. In case anyone is wondering, this will return the default value of the dropdown option, which is "Option A".
Answer by Jmather · Jan 06, 2020 at 12:53 AM
The following will give you the text from the selected dropdown item:
Debug.Log(EventSystem.current.currentSelectedGameObject.name);
Your answer
Follow this Question
Related Questions
How add(create) a new gameobject on scene on mouse button click. 2 Answers
Right Click Load Scene help! 1 Answer
DropDownList don't close at game paused 0 Answers
Reuseable Dropdown 0 Answers
How do change UI.button onclick variables via script?(JS) 1 Answer