- Home /
c# change what array I am calling dynamically
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)))[i]));
}
}
}
}
What are the possible array names that would go in here? Where is the rest of the code to show the full problem?
Sorry, here is an example of the arrays in a script. the values are set in the inspector for each item the script is attached to as they all do different things with the same items.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class $$anonymous$$achine$$anonymous$$enuScript : $$anonymous$$onoBehaviour {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Steel Products
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Assembler Parts
public string[] parts;
public string[] finishedParts;
//Steel Parts
public string[] steelSquareBlanks;
public string[] steelRectangleBlanks;
public string[] steelCircleBlanks;
public string[] steelBentSquareBlanks;
public string[] steelBentRectangleBlanks;
public string[] steelBentCircleBlanks;
public string[] steelCylinderTubing;
public string[] steelSquareTubing;
public string[] steelCutCylinderTubing;
public string[] steelCutSquareTubing;
Answer by dpoly · Jan 03, 2017 at 03:09 AM
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][i])
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.
Answer by Creeper_Math · Jan 02, 2017 at 06:38 PM
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.
Answer by RobAnthem · Jan 02, 2017 at 06:58 PM
In your dropbox class
public string[] dbList;
and your other class would have
FindObjectOfType<dbClass>().dbList = otherArray;
or
private string dbList[]
public void populateList(string[] myList)
{
dbList = myList;
for (int i = 0; i < myList.Length; i++)
{
//do stuff with array entries
}
}
Your other class file would have in it
FindObjectOfType<dbClass>().populateList(otherArray);
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.
Your answer
Follow this Question
Related Questions
3D arrangement of collection of gameobjects 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Add Listeners to array of Buttons 2 Answers
c# to access force with scroll bar 0 Answers