accessing array of arrays from another script

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:

  1. Does the static keyword mean that one cannot add anything to the array?

  2. 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?

  1. 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?

  2. 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.

Try this:

static var array1 = Array("hello","world");
static var array2 = Array("Unity","is");
static var arrays = Array(array1, array2);

(arrays[1] as Array).Add("cool"); //iPhone version

From Script2:

(Script2.arrays[1] as Array).Add("beans"); //iPhone version
Debug.Log(Script2.arrays);

Note that you can leave out ‘as Array’ if you’re not programming for the iPhone.

tonyd:

Thanks for your answer. Yes, this is for the iPhone. I’d forgotten about the “(… as Array).Add” approach.

I tried your suggestion as below,

static var array1 = Array();
static var array2 = Array();
static var arrays = Array(array1, array2); 
(arrays[0] as Array).Add("cool");

but when I hit Play, Unity crashes! The crashing could be because the code actually looks like:

static var array1 = Array();
static var array2 = Array();
static var arrays = Array(array1, array2); 
static var ArrayArray = [new Array(array1[0], array2[1])];
(arrays[0] as Array).Add("cool");

I’m guessing the crashing is because I am using array1 in ArrayArray before putting anything in it? I also tried

static var array1 : GemeObject[] = new GameObject[10];
static var array2 : GemeObject[] = new GameObject[10];
static var arrays = Array(array1, array2); 
static var ArrayArray = [new Array(array1[0], array2[1])];
(arrays[0] as Array).Add("cool");

But when I hit Play, I get the error for the fifth line:

“Object reference not set to an instance of an object.”

Is this because array1 has not been instantiated but only defined? The problem I have is that in reality I define array1 as empty, and then use code to fill array1 with instantiated GameObjects.

I also tried:

static var array1 = Array(); 
static var array2 = Array();
static var ArrayArray = [new Array(array1[0], array2[1])];
(array1 as Array).Add("cool");

as well as

static var array1 : Array = new Array(); 
static var array2 : Array = new Array(); 
static var ArrayArray = [new Array(array1[0], array2[1])];
(array1 as Array).Add("cool");

but when I hit Play, Unity crashes for both of these!

You’re trying to make ArrayArray as an array containing an array contain the first element of array1 and the second element of array 2, when array 1 and array 2 are both empty?

What exactly are you trying to accomplish with this line:

static var ArrayArray = [new Array(array1[0], array2[1])];

Because I’m not sure why the latter half is enclosed in brackets.

Also, you are asking for element 0 of array1 and element 1 of array2 when both arrays are empty.

Finally, remember there are two types of arrays, javascript arrays and .net arrays, and the latter can’t be resized.

One more thing… you might want to file a bug report with Unity.

Because your code shouldn’t cause Unity to crash, even if your syntax is wrong.

tonyd:

What I am trying to do is to instantiate groups of GameObjects, but I want to create arrays of GameObjects that are near each other. I figured this approach would allow for loading all of this information quickly at startup in the iPhone. So in function Awake(), I instantiate the GameObjects and add them to array1 and array2, but the nearest neighbor relationships between the GameObjects are already created by other arrays such as ArrayArray.

I guess I’m thinking of creating these arrays of arrays as creating relations between pointers and then when I instantiate the GameObjects, I load the data into the memory location pointed to by the beginning pointer.

Maybe this doesn’t work with Unity’s arrays and there is a simpler way to do this?

The following should work:

var bigList = Array(); //create parent/master array first
var nested1 = Array(); //create children/nested arrays second
var nested2 = Array();
var nested3 = Array();
bigList.Add(nested1, nested2, nested3); //add children to master array
(bigList[0] as Array).Add("nested element 1"); //add string to first array in master array
Debug.Log((bigList[0] as Array)[0]); //get first element of first nested array in master array

Be warned that nested arrays can get quite ugly on the iPhone.

tonyd:

That approach worked. Thanks!

What can get quite ugly about nested arrays on the iPhone?

Just the fact that you’ve always got to remember to cast it with ‘as Array’.

And it’s not so bad if they’re only nested one deep, but if they are nested 3 or 4 levels deep… your code could get quite ugly!