Does anyone know how to create arrays of arrays in javascript that can then be used from other javascript scripts? I have a fixed number of GameObjects that I want to instantiate and then add them to the Arrays, and then make an Array of these Arrays.
I find I can do this using static arrays:
Script1.js -
static var Array1 : Array = [GameObject_0, GameObject_1];
static var Array2 : Array = [GameObject_2, GameObject_3];
static var ArrayArray : Array = [Array1, Array2];
And I can use ArrayArray from another script, say in Script2.js I can
call ArrayArray as:
Script1.ArrayArray;
The problem I have is that it appears that I cannot add anything to the static arrays.
I have two questions:
-
Does the static keyword mean that one cannot add anything to the array?
-
How do I make an array of an array that I can use from another javascript script if for the first array I want to add things to it and then create the array of array. I.E. in the following code I make Array1, Array2 and then declare ArrayArray the same as above. But I get the error when creating ArrayArray “An instance of the script is required to access non static member Array1”.
var Array1 : Array;
var Array2 : Array;
static var ArrayArray : Array = [Array1, Array2];
Array1.Add(GameObject_0);
Array1.Add(GameObject_1);
Array2.Add(GameObject_2);
Array2.Add(GameObject_3);
It seems that I can only add things to an array if it is non-static, is that correct? How do I use “an instance of the script” as described in the error in this case?
-
Or do I need to make copies of the arrays that had things added to them into static arrays and then make the array of arrays from these static arrays?
-
Another approach I tried is
static var Array1 : GameObject[] = new GameObject[10];
static var Array2 : GameObject[] = new GameObject[10];
static var ArrayArray : Array = [Array1, Array2];
Array1.Add(GameObject_0);
Array1.Add(GameObject_1);
Array2.Add(GameObject_2);
Array2.Add(GameObject_3);
But I get the error "Add is not a member of GameObject.