How can I find a dropdown menu game object from a script?
Hello all, I've created a canvas with a dropdown menu in the editor, it's there sitting in my Hierarchy window staring at me, but I cannot for the life of me figure out how to reference that from script.
I need to pass the value of the drop down menu to a function so I assume I need SOMETHING.value but I don't know what that something is. I tried (Dropdown)GameObject.Find("myDropdown")
and was told that i can't convert from a game object to a dropdown :(
Answer by Rajeesh_AR · Nov 30, 2015 at 12:20 PM
Hi, I am not sure about your script. Still did you add "using UnityEngine.UI;" to the script. If not add that and check. If this doesn't solve your issue, please share your script, so that I will check and help you. Thanks.
The man is right. And you can't cast a GameObject as a Dropdown, since one is a gameobject ... and the other a dropdown CO$$anonymous$$PONENT.
So what you want to call is in fact:
Dropdown uiDropdown = GameObject.Find("myDropdown").GetComponent<Dropdown>();
Thanks, Rajeesh_AR and $$anonymous$$dRWaylander, your combined solution worked.
Answer by epistomai · May 25, 2016 at 10:34 PM
I was looking for this answer, I had exactly the same question. But now, how can I retrieve the value from the options instead of the value of the index? Meaning, if in the first option (index is 0) is "This" (string for example)
Answer by dzne_weiss · Nov 03, 2016 at 01:39 PM
You would do it something like that:
Dropdown uiDropdown = GameObject.Find("myDropdown").GetComponent<Dropdown>();
and then:
string chosenValue = uiDropdown.captionText.text;
Your answer
Follow this Question
Related Questions
UI Canvas draw order 1 Answer
Tutorial for Dropdown item needed 1 Answer
DropDown with Text Input capabilities 0 Answers
Search in Dropdown options 2 Answers
How would I go about comparing input from a InputField to a array? 0 Answers