How to change the choices of a PopupField after it has been created?

I’m using UI Toolkit to create a custom editor window and I want a PopupField that list all the children GameObjects of a specific GameObject, so I loop through all GO and add their names to a list of strings, and then i create a PopupField and uses that list in the constructor, something like this (simplified example):

List<string> choices = new List<string>() { "One", "Two", "Three" }

var myPopupField = new PopupField<string>("My PopupField ", choices, 0);

That works fine, but when I update the choices list with new strings the PopupField does not reflect the changes automatically. So do I need to reassign the list to the choices? I tried to do something like this:

choices = new List<string> { "Four", "Five" };

myPopupField.choices = choices;

But there is no .choices, or .options or anything like that. So how do I modify and update the strings that are shown in the PopupField?

Hello! The example you posted should work in the latest version of Unity. choices was indeed internal so you couldn’t set the choices after the PopupField creation, but now choices became public. Which version of Unity are you using? Are you using the built-in UI Toolkit or the package?

Unity 2020.3.16f1 and no, I don’t think I use the package, I think it is the built-in one. What version of Unity do I need and/or what package do I need? I would prefer to not use alpha or beta version, since we are in production.

@cpalma-unity I’ve just tried installing the package Version 1.0.0-preview.17, but the there are still no .choices available to use. So what version of Unity or the packages do I need in order to get this working?

@cpalma-unity Is there a way to get around this using Reflection? I’ve tried doing this:

myPopupField.SetPrivateFieldValue<List<string>>("choices", new List<string>() { "One", "Two", "Three" });

But I get the error : ArgumentOutOfRangeException: Field choices was not found in Type UnityEditor.UIElements.PopupField

(Edit)
The extension methods I use are presented here: Set TimelineClip.(pre|post)ExtrapolationMode from Script

Hey @KristofferH . The support for changing the choices after the PopupField creation is available in
the built-in UI Toolkit in Unity 2021.2.0a18 and above and it is not available in the UI Toolkit packages. About setting the value using reflection, you could try:

var prop = popupField.GetType().GetField("m_Choices", System.Reflection.BindingFlags.NonPublic
                                                              | System.Reflection.BindingFlags.Instance);
prop.SetValue(popupField, new List<string> {"One", "Two", "Three"});