I could ask a myriad of questions about arrays here, but I'll stick to one specific one. If you see other inefficiencies in the code below, please know that I am not trying to saddle you with the challenge of fixing that.
My specific issue is that I want to cycle through the whole length of my jagged menu[][] array, and as I go, extract a Vector3 value from my buttons[] array, and assign it to the Vector3[] array.
I am not sure why I can't do what I've posted below, but I get:
1) NullReferenceException error (on buttonPos[n] = buttons[n].transform.position,
2) "Field 'MatrixPicker.buttonPos' is never assigned to, and will always have its default value 'null'",
...when I try it. All my button[] GameObjects are located in the public fields where they belong in Unity.
Here is my code:
public class MatrixPicker : MonoBehaviour {
string[][] menu;
public GameObject[] buttons;
private Vector3[] buttonPos;
void Start () {
menu = new string[][]{
new string[]{"a"},
new string[]{"b"},
new string[]{"c", "d", "e"},
new string[]{"f", "g", "h"}
};
int tot = 0;
for (int i = 0; i < menu.Length; i++){
string[] innerArray = menu[i];
for (int a = 0; a < innerArray.Length; a++){
tot++;
int n = tot-1;
buttonPos[n] = buttons[n].transform.position;
}
}
}
//other code
}