- Home /
Multidirectional arrays on javascript
Ok, Unity 3.4 has support for multidirectional arrays on JS. But Im having troubles initializing them. Here's my example:
var myArray : Array;
function Start() { myArray = new Array(); myArray[0][0] = 1; myArray[0][1] = 2; myArray[2][3] = 3; myArray[2][4] = 4; myArray[3][0] = 5; }
But when I try to use it it seems to be empty. What Im doing wrong?
Console Error: "ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count."
Thanks!
Answer by Eric5h5 · Jan 05, 2012 at 06:35 PM
You mean "multidimensional", not "multidirectional". Array is obsolete and pretty much shouldn't ever be used. Use built-in arrays or Lists instead. A multidimensional array is declared like this:
var myArray : int[,];
function Start () {
myArray = new int[4,4];
myArray[0,0] = 1;
myArray[0,1] = 2;
}
Thanks for the clarification! ArrayLists is what you are telling me to use?
And thanks for the correction! Lost on translation! ;)
If we talk about not using things I would also mention "JavaScript" for Unity scripts. Take an hour and make yourself comfortable with C# :) It helps a lot keeping the code clean and readable.
This is my first project. The second one will be on C#. Thanks! :)
No, not ArrayList, they are just as obsolete as Array. You can use generic Lists if you need resizable arrays, otherwise standard built-in arrays are fine. @softrare: that's a matter of opinion, and personally I find Unityscript cleaner and easier to read for the most part. C# tends to be sometimes obtuse and unnecessarily fiddly, and not as well integrated into Unity (which is understandable, since Unityscript is custom made for Unity).
@Eric5x5, I now only use List since you told me to.
But in fact ........... I'm wondering, what do you recommend for 2-dimensional use, here in the Unity universe? (Let's say, we know in advance the fixed length of each dimension; and what if we don't). Thanks.
Softare .. Eric is right and you are wrong on this one. To phrase it highly offensively - and why not> this is the internet after all - only someone who is not a real programmer would find c# better than UnityScript. As far as program$$anonymous$$g languages go, they are both a laughable idiotic bizarre stupid joke. (In fairness, i think the same thing about every program$$anonymous$$g language and environment I've been forced to use.) And as Eric states, UnityScript is far more "integrated" with U3D. You could put it this way: I$$anonymous$$O a preference for C# is naive. This message brought to you by Flame War Central! :-)
incidentally ....... is your nickname (softare) an incredibly clever reference to the archaic measure of 2D space in mideuropean languages?? If so, you have the cleverest nickname on the system.
Answer by softrare · Jan 04, 2012 at 03:51 PM
does that work?
var myArray : Array;
function Start() {
myArray = new Array();
myArray[0] = new Array();
myArray[0][0] = 1;
myArray[0][1] = 2;
myArray[2] = new Array();
myArray[2][3] = 3;
myArray[2][4] = 4;
myArray[3] = new Array();
myArray[3][0] = 5;
}
That works perfectly! It's not a multidirectional array, is an array of arrays, but it works fine! Thanks!
Not a problem :) to be a smart ass once more: it's a multi"dimensional" array ^^
if I resolved your question please mark my answer accordingly (solved) :)
Your answer
Follow this Question
Related Questions
Best way to keep track of objects on a 3D Grid? 2 Answers
How to return the position of an object in a 2 dimensional array 1 Answer
How to debug values in jagged arrays? 0 Answers
IndexOutOfRangeException for initial spawn of prefabs from an array 1 Answer
Implementing a 2D array as a field on a ScriptableObject? 1 Answer