- Home /
trouble with 2 dimensional arrrays in javascript
I am having trouble declaring and using a 2 dimensional array in javascript using unity 3.3
var x2: int[,]; no error but then errors later
var x2:int[5,5]; //error excpecting semicolon at the end.
x2[4][4]=0;
Debug.Log(x2[4][4]); //error BCE0109: Array 'self.x2' is rank '2', not rank '1'.and
type int does not support slicing.
So many errors? I'm following examples other people have posted about the multidimensional arrays in javascript in 3.2
Any ideas? thanks Dan
Answer by SilverTabby · Aug 02, 2011 at 08:11 PM
Javascript 2d arrays are tricky. I ended up looking up C# 2d arrays and then moved the syntax to javascript and it worked. Here's what I found worked:
var myArray : int[,] = new int[5,5];
myArray[0,1] = 42;
for(var X : int = 0; X < myArray.Length(0); X++)
for(var Y: int = 0; Y < myArray.Length(1); Y++)
{
myArray[X,Y] = X+Y;
Debug.Log(myArray[X,Y]);
}
That should be all the information you need to work with and loop through 2d arrays.
Yep. That works. They should update the docs under arrays with your suggestions about how to use them.
Fascinating that the "length" is a kind of a function across the dimensions!! Would never have guessed, and it's totally undocumented of course. Cheers.
The length calls in unityscript gives me... BCE0077: It is not possible to invoke an expression of type 'int'. What's up with that?
Thanks Eric5h5! GetLength compiles. But where is that documented? Even after knowing the proper semantics, I'm having trouble finding a definitive entry within language docs. I assume there may be other functionality attached to multidimensional arrays. Where can I find the complete set available to unityscript?
Your answer
Follow this Question
Related Questions
Error with Array declaration 1 Answer
Access specific GameObject in arrays 1 Answer
How to declare and initialize multidimensional arrays JS? 4 Answers
Vector 3 in array 1 Answer
How to declare a multidimensional array of strings in c#? 2 Answers