Ok, I am calling in a 1D array to populate a dropdown list and I basically want to be able to change the array I am calling in from the other script with some sort of variable. How would I go about this? Would I need Dictionary?(Don’t really understand them yet). Thank you for any help!
void OnTriggerStay(Collider other) {
if (other.tag == "Player") {
//Key press to bring up menu
if (Input.GetKeyDown (KeyCode.E)) {
Debug.Log("getting menu");
//Find the dropdown
Dropdown uiDropdown = GameObject.Find ("MachineDropDown").GetComponent<Dropdown> ();
//Get rid of old info
uiDropdown.options.Clear();
//Populate dropdown
for (int i = 0; i < 2; i++) {
uiDropdown.options.Add (new Dropdown.OptionData (gameObject.GetComponent<MachineMenuScript>().(((VARIABLE NAME OF ARRAY)))*));*
You can’t use ‘variable names’ but you can index into an array of arrays.
var arrays = new string[][] {
steelSquareBlanks, steelRectangleBlanks, steelCircleBlanks // etc
};
var x = 1; // choose the steel rectangles
var opt = new Dropdown.OptionData(gameObject.GetComponent<MachineMenuScript>().arrays[x]*)*
uiDropdown.options.Add(opt); I like to use var a lot to break expressions into pieces that are easier to read. This is just skeleton code to give the idea.
First of all, instead of the "for (int i = 0; i < 2; i++), I would use a “foreach”, which just loops around with a variable “optiontext” (default being “item”) that is set to the data in the array input each time
string[] OptionFiller = // The string array of options u want the dropdown menu to have
foreach(string optiontext in OptionFillerArray) {
uiDropdown.options.add(new Dropdown.OptionData(item));
}
The way I would do it would be way to complex, and still requires you to manually input all the arrays into a function that you need to get. I don’t know about Dictionaries, so I will leave that for someone else to answer if they want.
To be 100% clear, C# DOES NOT support dynamic variable names, because var names are only as meaningful as the uncompiled code they exist in, the moment they hit collections, that variable name is meaningless.