Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
4
Question by Zeronev · Sep 13, 2015 at 05:09 PM · c#uiunity5dropdownnew-feature

How to use the new Dropdown Menu?

Im very familiar with the unity 5 UI, especially with buttons, the way buttons work are calling a method... But I don't seem to find nor have any idea how to call a Method when clicking a certain option in the dropdown.

I have my own method called UseItem() and needs to be used when the first option in the dropdown is clicked. How can I achieve this? An example code or a reference would be great! Thanks, in advance :D

Comment
Add comment
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

2 Replies

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

Answer by Kwarts · Oct 06, 2015 at 08:41 AM

Here's a quick example in C#

 using UnityEngine;
 using UnityEngine.UI;
 
 public Dropdown myDropdown;
 
 void Start() {
     myDropdown.onValueChanged.AddListener(delegate {
         myDropdownValueChangedHandler(myDropdown);
     });
 }

 void Destroy() {
     myDropdown.onValueChanged.RemoveAllListeners();
 }
 
 private void myDropdownValueChangedHandler(Dropdown target) {
     Debug.Log("selected: "+target.value);
 }
 
 public void SetDropdownIndex(int index) {
     myDropdown.value = index;
 }


Comment
Add comment · Show 5 · 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 Hupman · Dec 01, 2015 at 02:27 AM 1
Share

This helped way more than the Unity documentation. I found all that was really needed is the 'myDropdownValueChangedHandler' that receives UnityEngine.UI.Dropdown. The rest is easily wired up in the editor :

$$anonymous$$ake a gameObject that uses the above script. Lets call it 'DropDownTarget'

add OnValueChanged event to the drop down

drag the 'DropDownTarget' to the event to receive the event notification

In the function drop down, select DropDownTarget->myDropdownValueChangedHandler

Drag your drop down object to the function arg.

avatar image theOtherHolmes Hupman · Nov 10, 2016 at 03:10 AM 1
Share

But doesn't myDropDownValueChangedHandler need to public, not private? Otherwise it won't show up in the function dropdown on the OnValueChanged property drawer.

avatar image jina771 · Jul 18, 2016 at 04:49 PM 0
Share

how does this work when your myDropdown object is not initialized? thanks!

avatar image brunocoimbra jina771 · Jul 18, 2016 at 05:42 PM 1
Share

Please, open a new question showing more info about your own problem. Unity Answers it NOT the Forums, and should not be used like that.

avatar image tbigfish88 · Aug 20, 2016 at 10:18 PM 0
Share

Wait, do I need to add a listener to even get it to drop down? I wasn't having a problem reading the resultant value, it just didn't function at all.

avatar image
2

Answer by DiegoSLTS · Sep 13, 2015 at 06:59 PM

Look here: http://docs.unity3d.com/Manual/script-Dropdown.html

You have to set a callback for the "OnValueChanged" event. That function will get an int as a parameter, which is the index of the selected item in the Dropdown.

Comment
Add comment · Show 3 · 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 stereoplanet · Oct 03, 2015 at 02:27 AM 0
Share

Is there a tutorial somewhere for this? I'm also trying to figure this out.

avatar image Suddoha stereoplanet · Oct 03, 2015 at 02:34 AM 0
Share

I haven't looked for tutorials but if you post a question here on Unity Answers or in the forum with your desired effect or description of the information you need, people will surely get you started.

avatar image tbigfish88 · Aug 20, 2016 at 10:18 PM 0
Share

Wait, do I need to add a listener to even get it to drop down? I wasn't having a problem reading the resultant value, it just didn't function at all.

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

40 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

Related Questions

Remove specific screen resolutions 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Forcing a program to initialize inside Unity game. 1 Answer

How to get a dropdown menu to change the colour of a reticule on another scene 0 Answers


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