Array of arrays, what am i doing wrong?
Hello trying to disable all gear on an AI. Each "class" is an array of gameobjects and i make an array of those arrays to disable ALL classes. But later i need to select a specific class with an index thats why im using an array of arrays. But first i try to understand this "dubble" array. In the start function i set each class in the "allGear" array which is the array of all arrays. But i get an error. What am i doing wrong?
Thanks
[Header("Equipment for each profession")]
public GameObject[] noProfession;
public GameObject[] milita;
public GameObject[] mason;
public GameObject[] carpender;
public GameObject[] archer;
public GameObject[] scribe;
public GameObject[] hunter;
public GameObject[][] allGear;
[HideInInspector]
public int professionIndex = 0;
//rankIndex
//1 = hunter
//2 = mason
//3 = carpender
//4 = milita
//5 = archer
//6 = scribe
void Start ()
{
allGear [0] = noProfession;
allGear [1] = hunter;
allGear [2] = mason;
allGear [3] = carpender;
allGear [4] = milita;
allGear [5] = archer;
allGear [6] = scribe;
}
public void UpdateGear (int index)
{
//Disable all gear;
for (int j = 0; j < allGear.Length; j++) {
for (int i = 0; i < allGear [j].Length; i++) {
allGear [j][i].SetActive(false);
}
}
}
What is the error?
EDIT: Nvm.. After a second look it is a null reference, because your array is not initialized.
public GameObject[][] allGear = new GameObject[7][];
consider using System.Linq;
And just do:
//Disable all
foreach (var gear in allGear.Select$$anonymous$$any(x => x))
{
gear.SetActive(false);
}
Enable all by profession id
foreach (var gear in allGear.First(x => x == archer))
{
gear.SetActive(true);
}
Answer by fluxhackspro · Apr 21, 2018 at 05:56 AM
Thank you @Positive7 for the correct solution! After a second look it is a null reference, because your array is not initialized.
public GameObject[][] allGear = new GameObject[7][];