Unity DropDown UI - Select/Deselect option inside a dropdown via C# script

Hi everyone!
I am trying to implement a piece of code that selects/deselects options inside a dropdown via script.
I already know how to capture a dropdown selection via mouse click using an OnValueChanged listener.

I’ve researched the methods available on the Unity reference and there are Select(), OnSelect() and OnDeselect() methods there.
However, I have no idea how to implement them in practice and how they work!
The reference only specifies a very general code:

public void OnDeselect (BaseEventData data)
{
Debug.Log (“Deselected”);
}

I have tried to “run” this function simply typing in:
myDropdown.OnDeselect();
But it says there is a problem with the arguments (I can see that there is a BaseEventData cell but have no idea to what it corresponds).
I also tried to type in:
myDropdown.OnDeselect(0);
which I thought would pass in 0 for the “data” variable and thus deselect the indexed 0 option…?
Anyways, I am completely lost as to how to select/deselect options inside a dropdown.

Any help would be much appreciated!
Thanks! :slight_smile:
Marma

PS: I am not talking about adding or clearing options inside dropdowns using “clearOptions” method…

2 Likes

OK, it was actually very simple, yet unclear…
Simply set the “myDropdown.value = 0;” and the selection is reset. :wink:

9 Likes

But I want no option to be selected…

For context, I’m making a drop down of say a list of subjects. And one of the options of the drop down holding string “Create New Subject” acts as a button. When it is selected, the value of drop down changes which triggers a method. This method checks if the selected option holds a string named “Create New Subject”. If yes then it pops up a window to prompt for the name of the new subject which is then added later on.

Problems arising:
Let’s say I don’t have any subjects at the start and there is only one option in the drop down (i.e. "Create New Subject). But if I want to create a new subject then the value of the drop down must not be equal to the index of the option “Create New Subject” (i.e. 0) so that a change of value would trigger the method. I solved this problem by setting the value of drop down in the inspector to anything but zero.

But if the user is midway through choosing the name and decides to quit, no new subject is added and the value of the drop down has already been set to 0 when the user chose the option to create a new subject. Now I can’t even forcefully change the value of the drop down to something other than zero through script since there is only one element in the options and even if I try to it just overwrites it to zero.

Solutions I’ve come up with:
The solutions that I’ve come up with are just work arounds; I decided to remove the close button when the user is first creating a subject or may be forcefully create a subject from the user during a tutorial may be. This is only temporary for now; if anyone knows a solution to this let me know.

Any help would be appreciated