Help with arrays of arrays please
I am trying to organize all my button texts into a neat array of arrays, I want the below to be put into a 6 element array but I cant get the syntax right.
public Text[] SwordTexts = new Text[6];
public Text[] ArmourTexts = new Text[6];
public Text[] StaffTexts = new Text[6];
public Text[] RingsTexts = new Text[6];
public Text[] AmuletTexts = new Text[6];
public Text[] CrystalTexts = new Text[6];
Then the array of arrays:
Text[,] AllTexts = new Text[6, 6];
Then in my start function:
Text [,] AllTexts = new Text[,]{SwordTexts, ArmourTexts, StaffTexts, RingsTexts, AmuletTexts, CrystalTexts};
This line gets "A nested array initializer was expected" and I don't understand why
I have also tried
Text[][] AllTexts = new Text[6][];
Text [][] AllTexts = new Text[][]{SwordTexts, ArmourTexts, StaffTexts, RingsTexts, AmuletTexts, CrystalTexts}
AllTexts[0][0].text = "hi";
And this gives me an object reference not set to an object error
Answer by Statement · Oct 25, 2015 at 09:41 PM
I think you must create the jagged array in Awake. This works.
using UnityEngine;
using UnityEngine.UI;
public class Neat : MonoBehaviour
{
public Text[] SwordTexts = new Text[6];
public Text[] ArmourTexts = new Text[6];
public Text[] StaffTexts = new Text[6];
public Text[] RingsTexts = new Text[6];
public Text[] AmuletTexts = new Text[6];
public Text[] CrystalTexts = new Text[6];
private Text[][] AllTexts;
void Awake()
{
AllTexts = new Text[][]
{
SwordTexts,
ArmourTexts,
StaffTexts,
RingsTexts,
AmuletTexts,
CrystalTexts
};
}
}
Answer by Dinosaurs · Oct 25, 2015 at 09:10 PM
The C# documentation has the format for multidimensional array initializers: https://msdn.microsoft.com/en-us/library/aa664573(v=vs.71).aspx
int[,] b = {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};
That format seems like it'll be awkward for your data so I'd probably just set the indices manually:
Text[,] AllTexts = new Text[6, 6];
AllTexts[0] = SwordTexts;
etc.
That produces Error CS0022 Wrong number of indices inside []; expected 2