Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
9
Question by AlvinHerawan · Apr 08, 2016 at 09:52 PM · javascriptuidropdown

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);}
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Thoen · Apr 12, 2016 at 03:07 PM 0
Share

Hello !

 LocationPickerLabel.GetComponent<Dropdown>().GetComponentInChildren<Text>().text

:D

avatar image AlvinHerawan Thoen · Apr 12, 2016 at 03:10 PM 0
Share

Thanks, that may work, too. I haven't tried it, though.

avatar image MFKJ · Dec 06, 2019 at 05:57 AM 0
Share

In a 4 $$anonymous$$utes tutorial you can learn it easily https://www.youtube.com/watch?v=FteXwEdED0Y&t=2s

avatar image MFKJ · May 04, 2020 at 05:44 AM 0
Share

How to get dropdown value https://www.youtube.com/watch?v=FteXwEdED0Y

6 Replies

· Add your reply
  • Sort: 
avatar image
51
Best Answer

Answer by rsouza · Jun 15, 2017 at 03:10 PM

You can simply do this:

 Debug.Log(Dropdown.options[Dropdown.value].text);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wichenstaden · Jun 21, 2017 at 01:24 PM 3
Share

The logic behind this is solid. It's extremely simple and works like a charm. Thank you.

avatar image
7

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;
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlvinHerawan · Mar 09, 2017 at 10:02 AM 0
Share

Thanks for simplifying it up.

avatar image andrenospam0 · Dec 09, 2019 at 02:05 AM 0
Share

Unless for temporary use, it is better to work with the index. Problem will arise when globalizing your text!

avatar image
2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bkachmar · Jun 07, 2017 at 05:22 PM 0
Share

Doesn't work when you manually override the label. In my case it says "Please select your instrument", but Piano is selected by default.

avatar image AlvinHerawan bkachmar · Jun 07, 2017 at 05:37 PM 0
Share

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?

avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlvinHerawan · Apr 08, 2016 at 10:44 PM 0
Share

Thanks for the quick reply. In case anyone is wondering, this will return the default value of the dropdown option, which is "Option A".

avatar image
1

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

84 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity dropdown automatically scrolling up 0 Answers

Animated Icon in Unity Dropdown Menu 0 Answers

Trying to list names from text files in dropdown options.,Trying to list info from txt files. 0 Answers

How to add more UI elements to Dropdown OptionData except default string and sprite values in Unity? 0 Answers

Dropdown error 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges