Multidimensional typed arrays

The code below works in unity 2.x regular

var listje = new Array(10);
listje[0] = new Array(10);
listje[0][0] = “blah”;

It does not seem to work in unity for iphone. Most possibly because unity iphone doesn’t want dynamically typed stuff…

But can somebody please tell me how I could correctly use this multidimensional array in unity for iphone? (its JS by the way)

Thanks,

Bart

Hi Bart,

It appears that the unity iPhone engine doesn’t like this assignment operator:

listje[0][0] = "blah";

It keeps throwing an error “Type ‘Object’ does not support slicing.”

One way I could get this to work was to do the following:

var listje : Array = new Array(10);
var list2 : Array = new Array(10);
list2[0] = "blah";
listje[0] = list2;

Then whenever I wanted to get a value from list2 I had to do it this way:

var tArray : Array = listje[0];
Debug.Log(tArray[0]); // "blah"

Hope that helps,

iBrent

I’d guess something like this would do?

Javascript:

var MyArray: Array[];
MyArray = new Array(10);
MyArray[0] = new Array();
MyArray[0].Push("hello");
MyArray[1] = new Array();
MyArray[1].Push("world");

Debug.Log("array: "+ MyArray[0][0] +" "+ MyArray[1][0]);

Where type Array[ ] is used to declare the multi-dimensional array.

Hello

The Push solution provided by Proto works on bi-dimensional arrays. Thanks!

What would be necessary to populate a tri-dimensional array? I tried to extrapolate a working code but always get the same error.

you need nested loops to populate an array of array of array (its better calling it that way as that makes clear what you have structure wise).
What you basically have is pushes of whole arrays into the array- again and then push the elements to those array[×][y] array objects

On the iPhone, you must cast the result of the first index as an Array:

(listje[0] as Array)[0] = "blah";
Debug.Log( (listje[0] as Array)[0] );

Ugly, but it works.

Thanks dreamora

Can you please help me with a code snippet. I followed your instructions but no results yet…

static var ActiveArray : Array[];

for (x =0; x<10; x++) {
    ActiveArray[x] = new Array(10);
    for (y =0; y<10; y++) {
        ActiveArray[x].Push(new Array(10));
         for (var z =0; z<10; z++) {
            ActiveArray[x][y].Push(true);
        }
    }
}

Huh ? Why are you doin that Z loop if you’re using the Y loop into it ? :roll:

( Here, your code just writes 10 times the same " ActiveArray[×][y].Push(true); " )

The Push is supposed to place a new value into the next empty space on the [×][y] Array. In other words it is basically populating the 10 z-values.

I believe you are mixing up the array class and .net arrays in the way you are accessing them.

multidimensional arrays can be done fairly easily using loops like this:

for (var y = 0; y < gridY; y++) 
	{
		yArray[y] = new Array(gridX);
        for (var x=0;x<gridX;x++) 
		{
			var pos = Vector3 (x, 0, y) * spacing;
			yArray[y][x] = Instantiate(prefab, pos, Quaternion.identity);
        }
    }

in my example I just fill the arrays with prefab objects. you would need to define the prefab, spacing.

read about arrays here if you like