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:
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.
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.
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:
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!