- Home /
How to make dynamic dropdown to choose items in an Array?
Hi, I'm wondering how exactly I could go about making a dynamic dropdown menu for a really big gameobject array I have.
I made 107 gameobjects for a building game prototype I recently started. I loaded them all into a GameObject array called ghosts, and i can cycle through them by changing the value of the array, like ghosts[0] to ghosts[1] or ghosts[2] etc. I am wondering if there is a way to put these objects into a ui dropdown list, and most importantly if i can set each dropdown option to change my array value, so if I click the third down option in the list it can change ghosts[0] to ghosts[2].
I'm open to whatever suggestions anyone comes up with, I just don't know exactly how to do this.
Answer by Kishotta · Jul 24, 2017 at 05:10 AM
You can use something like this to programatically populate a Dropdown:
void PopulateDropdown (Dropdown dropdown, GameObject[] optionsArray) {
List<string> options = new List<string> ();
foreach (var option in optionsArray) {
options.Add(option.name); // Or whatever you want for a label
}
dropdown.ClearOptions ();
dropdown.AddOptions(options);
}
usage:
Dropdown myDropdown;
GameObject[] dropdownObjects = GameObject.FindGameObjectsWithTag ("PowerUp"); // For example
PopulateDropdown (myDropdown, dropdownObjects);
Note: This can be pretty slow if you have a ton of items. Because Dropdown.AddOptions()
doesn't get rid of any existing options, you may be able to do this one item at a time in a coroutine as opposed to doing it all at once.
I will try this out in the morning, thank you! On my own I was trying to convert my object array to a list with each array objects name, but it doesn't seem to be going anywhere. Thanks much for the suggestion!
Ok I tried this out and it did the first part exactly as I wanted it to. I think I know how to make it so that each selection sets my array[var] to that specific object. If you want to try and help me with that you can, but until then I am accepting this as the answer!
@$$anonymous$$ishotta if you get this, I am wondering why the dropdown.AddOptions and ClearOptions are returning a "Could not find reference" error, even though after unpausing the game after this error it works perfectly fine?
the code by itself works fine, but I went into my other project and it started throwing this weird error every time I want to use the PopulateDropdown function, I just have no idea why.
void PopulateDropdown(Dropdown dropdown, GameObject[] optionsArray)
{
List<string> options = new List<string>();
foreach (var option in optionsArray)
{
options.Add(option.name); // Or whatever you want for a label
}
dropdown.ClearOptions();
dropdown.AddOptions(options);
}
private void Awake()
{
Dropdown myDropdown = GetComponent<Dropdown>();
GameObject[] dropdownObjects = Resources.LoadAll<GameObject>("Prefab/Ghost/Ghost " + path) as GameObject[]; // For example
PopulateDropdown(myDropdown, dropdownObjects);
}
this is where i call it, but it throws the stupid reference error, even though not only does it work, but it has no reason to not find the Dropdown in reference.
Odd that it would be erroring out but also working....
Can you post the complete error you get from the console?
NullReferenceException: Object reference not set to an instance of an object $$anonymous$$enu.PopulateDropdown (UnityEngine.UI.Dropdown dropdown, UnityEngine.GameObject[] optionsArray) (at Assets/Script/$$anonymous$$enu.cs:23) $$anonymous$$enu.Awake () (at Assets/Script/$$anonymous$$enu.cs:31)
the line 23 would be dropdown.ClearOptions();, but if i get rid of that it moves to .AddOptions so i figure for some reason it cant find the dropdown? but that makes no sense because it finds the dropdown, and populates the list. line 23 is just calling the function.
Answer by codemaker2015 · Sep 22, 2021 at 01:03 PM
You can use AddOptions()
method to add dropdown options dynamically. There we have specify a list of string that needs to display on the dropdown.
[SerializeField]
Dropdown dropdown;
public void Start() {
List<string> options = new List<string> () { "Option1", "Option2", "Option3" };
AddDropdownOptions(dropdown, options);
}
void AddDropdownOptions(Dropdown dropdown, List<string> options) {
dropdown.ClearOptions ();
dropdown.AddOptions(options);
}
Your answer
Follow this Question
Related Questions
dropdown uses a generic list and not a built-in list -- why? 0 Answers
instantiate gameobjects inside a canvas 0 Answers
Gathering GameObjects and then creating a button for each GameObject?? 2 Answers
New Unity Dropdown Menu to Change Game Resolution 1 Answer
Use an objects (from array) position to focus a camera on 3 Answers