- Home /
How to make a Jagged Array global?
I just found out in my last question that I can only define / initialize a jagged array in a function.
I need to be able to make a jagged array exposed globally though!
Unity doesn't seem to like this:
var my JaggedArray : int[][];
I throws errors at me when I write that, but it is the only way I see to expose it globally!
Please help. This is the final piece I need to get what I want up and running!
Thanks! - YA
Answer by Eric5h5 · May 22, 2013 at 03:49 AM
Unityscript doesn't have the syntax required to explicitly define the type of a jagged array. If the array is not actually jagged, you can use a 2D array:
var myArray : int[,];
Otherwise you can make a List of int[] arrays.
var myArray : List.< int[] >;
Another possibility would be to define a jagged array implicitly using dummy values just to get the type, then assign actual values elsewhere as necessary.
var myArray = [[1]];
function Start () {
myArray = [[1, 2, 3], [4, 5], [6], [7, 8, 9]];
}
Thanks! I ended up using Generic Lists and utilizing to ToArray function to make a jagged array from it. This is a bit off topic, but what kind of array is a [[]]? I have never seen that! (Only [,], [][])
It's a jagged array, like I said. It's implicitly declared using the $$anonymous$$imum number of elements.