Unable to change Dropdown.value

I just don’t know if I’m completely ignoring some very basic stuff, but I’m getting a weird behavior when trying to set the Dropdown.value property.

void SelectCategory (int index){
       
  ...

  categoriesDropdown.value = index;
       
  Debug.Log ("index = " + index + "; dropdown value = " + categoriesDropdown.value);

  ...

}

The method SelectCategory is called by the onValueChanged event from the dropdown object, set by code in this way:
categoriesDropdown.onValueChanged.AddListener (SelectCategory);

I have tried to call the method from the script in two ways:

SelectCategory(0);
and
categoriesDropdown.onValueChange.Invoke(0);

When the current dropdown.value, before the call, is 1, I get:
index = 0; dropdown value = 1

The value doesn’t change!?

If I change the line:
categoriesDropdown.value = index;
to
categoriesDropdown.value = 0;

Since the default value is 0, I’m just unable to change the value of the dropdown by clicking on it, so that, in this case (when the current dropdown.value is equal to the index value) the variable assignment line seems to be effective, i.e. it seems to be effective in preventing the value change, but not in changing it from a previously different value. What am I missing here? Is there another way to change the selected dropdown value in the script?

I’ve yet to encounter any problems simply changing dropdown.value.

It is however, rather strange that your “categoriesDropdown” variable is tinted blue in the code tag. Try renaming your variable, probably not the issue but just eliminating a possibility.

No problem with the name of the variable. And the problem repeats itself with other variables. I just can’t figure it out. I have tried to assign this value in a number of ways, but it just refuses to change. The debug line comes just below. Something is making it stick to the previous value.

Initially, I thought this code would create a loop, since setting a new value should call the listening method again and again, but it doesn’t seem to be so. If I change the categoriesDropdown.value outside the method, the method doesn’t run, even if it is listening to onValueChange.

EDIT: I have been able to make it work, even without really knowing what prevented it from working previously. If I set the value from outside the method before invoking onValueChange, it works well:

categoriesDropdown.value = index;
categoriesDropdown.onValueChanged.Invoke(index);

void SelectCategory (int index) {
   Debug.Log ("index: " + index + "; dropdown value: " + categoriesDropdown.value);
}

Why are you trying to change the value in the event that is raised when the value changes?

I had tried to do it outside also, without success. But I did it from another event raised from another dropdown, calling the SelectCategory method directly. I found out that changing it from outside and invoking the event did the job.

I have the problem too :||

I’m having the same problem.

Same problem here. Even just setting (through script) the dropdown.value to an integer (like 4 for instance), the value ingame doesn’t change to the correct value that was set through script. I triple checked if there was any code that was resetting it to 0 but there’s none.

Same problem here. I tried changing other things related to the dropdown’s gameobject and it successfully changed, but the value does not change :frowning:

I was having the same problem with the TextMeshPro version (TMP_Dropdown), but I solved it using an IEnumerator to add a pause between selecting the dropdown (for some reason, this was required in order to change the value) and setting its new value:

TMP_Dropdown dropdown;

IEnumerator ChangeValue(int newValue){
    dropdown.Select();
    yield return new WaitForEndOfFrame();
    dropdown.value = newValue;
    dropdown.RefreshShownValue();
}

As far as I’ve seen, the regular version of Dropdown has the same variables and methods of the TMP version, so it might work for that one as well. Hope this helps!

2 Likes

Thank you so much, this worked for me!

1 Like

Looks like DropDown.value can’t be changed via code when it already has callback. I’ve fixed the same problem with the following code:

Dropdown.onValueChanged.RemoveAllListeners();
Dropdown.value = ...;
Dropdown.onValueChanged.AddListener(...);
1 Like

This still does not work for me (tried all of the solutions), code is solid, debugged it, value just refuses to update

1 Like