Can't populate Dropdown at runtime

I can’t seem to populate a Dropdown at runtime. It appears greyed out and I can’t interact with it.

 container= rootVisualElement.Q<VisualElement>("team_one_container");
dropdown = container.Q<DropdownField>("team_one_character_dropdown");
      
dropdown.choices.Add("option one");

I’ve already debugged the code and the dropdown variable loads successfully.

The choice list can be edited after the fact, new items will be in the dropdown menu next time it is opened. BasePopupField.choices.set is virtual public, has been for a while, but there were some other changes in the code over time. What version are you using?

When testing here, I am able to see the option added. Does this work on your end? (when you create+ add a choice straight after)

I am using unity 2021.2.11.f1 with the default ui toolkit.

I don’t know what window are you showing me…

I have a MonoBehaviour in which I execute my code at runtime to edit a UI template with an existing DropDown element

thanks for your reply

It is just a dummy test window where I create a dropdown with a few choices, and then add the “Test 5” option. (You can see the relevant code in the background). The Test 5 option is on the list and not disabled.

You will need to share more (the template, all relevant uxml, uss, c#) so that we could investigate this. You could also fill a bug with your project and clear steps to reproduce the issue for someone else at unity to investigate this, but it usually takes longer to get an answer than the forum.

1 Like

It works for me, but what I do is that I initialize the choices like so:

dropdown.choices = new List<string>();
1 Like