❔ ✅ Update Dropdown options in Unity without loosing selected option
I have written a StatSelector class. The purpose is to let the user assign generated values to his character stats. It contains several dropdowns. A value can only be assigned once. Because of that I want to update all the dropdowns' options to remove any options I have selected in another dropdown already. Currently how I do it is iterate all the dropdowns except the one whose value was changed and clear it's options, then readd the options minus the selected option. This makes it so that any dropdowns that were already chosen forget which option was selected. How can I fix this?
32 Replies
I wish there would be a
RemoveOption()
method on the dropdown that would let me remove an option without clearing and readding all the options. Then my dropdowns would not forget what options was selected thoThe moment that I choose an option in a dropdown, all the others will get reset and loose their selected option.
well yeah, read the logic you are doing
that first one removes all options.
I know but I can't find an alternative approach to change the other dropdowns' options other than clearing them and readding
Well, that approach does come with the side effect of clearing all other dropboxes currently selected value 🙂
so either figure out a new way, or a workaround
that's pretty much what I asked for - A different way to update dropdown options other than clearing and readding
RemoveOption()
would be great, but there is no method like thatJust off the top of my head, maintain two lists (available, selected).
Whenever a value is selected, remove the selected value from (available), add it to selected, and for all currently empty dropdowns, re-calculate their values
you'll need a "reset" button or similar, if you want to start from scratch
is this legacy dropdown? if so, you can do this
Dropdown class uses List so you must iterate it
either way you should track your selected option somewhere else like in a List or just a property
it should be the legacy Dropdown yes, but apperently there is no RemoveAt() method
does it have any
Remove...
methods at all?nop
but yeah, please manage your state elsewhere and just let the UI components reflect your state. dont have them be source of truth
sorry, forgot misedd little details there... do this
myDrop.options.RemoveAt(i)
some kinda
OptionCollection
?that works, thank you
its a
List<Dropdown.OptionData>
oh, a normal list? yea then it has both remove by index and by value
works as expected now, thank you both very much ❤️
aight, goodluck
don't forget to $close
Use the
/close
command to mark a forum thread as answeredI think I do already but I'm open for improvements. The state that the UI is supposed to display is represented by
stats.values
and managed by the Stats
type. It generates random values for these different statistics as long as the sum of those values is lower than 60. The StatSelector
is only supposed to display which stats were generated and let the user assign them. Then if he clicks Select
my StatSelector
calls a factory method passing what the user has selectedyou can store the selected index
myDropDow.value
and later on you can just access the indexer myDropDown.options[myIndex]
now I'm not sure if I shall rather create a new thread for this but I'll just ask it here. I have an issue with my logic to remove selected options from the other dropdowns. If I compare the options by index there is the issue that after options got removed from certain dropdowns, same index may resolve to different options in different dropdowns. If I compare the options' strings by value, it also doesn't work properly because a value may appear in multiple options, for example there might be three options with the value
"11"
, if i compare by value i would remove all of these options instead of the selected one from the other dropdowns. So what to do instead? Maybe compare the strings by reference? I've never done that 🤔
@Pobiega @SlimStv any ideas what I can do in that case?Have a list be all available values, remove from that as things are selected? For dupe values, you only remove one of the value
So it still has a valid state
but then that brings me back to the clearing and readding every dropdown on any change doesn't it? Rather than Removing an option
yeah, but you would only clear the ones that doesnt have a selected value
eh would give so-so user experience for swapping things around
idk
UI logic is not my thing
I had it like that but changed to what I have now for some reason. I'll try that again then 🤔
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.