Square arrays in Javascript

I’m used to Java, where you can make the declaration:

int[][] squareArray = new int[5][5];

Is there a way to use builtin arrays this way in Unity?

var myArray = new Array(
    new Array(0, 1, 2),
    new Array(1, 2, 3)
);

Ok, that’s good for Javascript arrays, but is there no way to do it with builtins?

It may have been fixed, but early on in Unity’s life I was advised by someone at OTEE not to use two dimensional arrays. It was causing serious errors in my code.

Instead, use a single dimensional array but TREAT it like a two dimensional array. The size would be length * width. Traverse like this:

for (y=0; y<height; y++)
{
    for (x=0; x<width; x++)
    {
        MyArray[y * width + x] = whatever;
    }
}

might seem weird at first, but… really it’s no big deal.

Yep…I was experimenting with various ways of doing 2D arrays a couple weeks ago, and ended up doing what Aaron just demonstrated. It’s by far the fastest. Anything else seemed to involve dynamic typing, which can be noticeably slower when dealing with large amounts of array operations.

–Eric

That’s a good point.

Aaron: Nice idea and much faster! Thanks for sharing that!