- Home /
scripting with dropdown menu label
I want to do something like
if(*selected text in a dropdown menu* = "temperature")
{
callfunction()
}
how do I call text from dropdown menu ? thanks in advance.
Answer by UnityCoach · Jul 29, 2018 at 08:30 AM
If you use a dropdown ui (Create/UI/Dropdown), you can add text for every option. Then you can add a listener to the OnValueChanged event, that method is meant to take in an integer.
public void method DDValueChangedHandler (int selection)
{
switch (selection)
{
case 1 :
CallMethod();
break;
}
}
and if you really want to query the option label, you can always use :
dropdown.options[selection].text
hi thanks for your time. but I dont get it. please be more simple. I'm a newbie to unity and progra$$anonymous$$g in general.
Unity Answers is not the place to $$anonymous$$ch you how to program. @UnityCoach provided you with a good answer. Please look up other resources on program$$anonymous$$g in C#, and with Unity.
I am going to set @UnityCoach answer as correct.
Answer by unity_5FqH0gfyPcnrBw · Dec 05, 2020 at 06:20 AM
For those having trouble with TMP Drop down Menus.
using TMPro;
public class NewDropMenu : MonoBehaviour
{
public void DDValueChangedHandler()
{
int selection;
selection = gameObject.GetComponent<TMP_Dropdown>().value;
switch (selection)
{
case 0:
Method();
break;
case 1:
AnotherMethod();
break;
}
}
}
Your answer
Follow this Question
Related Questions
How do i make a dropdown via script(not using the new UI) 1 Answer
Dropdown captionText property - Default selected value doesn't exists 0 Answers
ScriptableObject Drop-Down Menu with added variables? 1 Answer
Unity Drop down menu negative value assign 1 Answer
How to tell if a Canvas UI slider is being clicked not over Value 2 Answers