- Home /
How to Create Multidimensional Array in Javascript?
I realise this question has been asked before, but I was unable to find any answer relating to the latest version of Unity except these two, which offer two different solutions, and I'm not sure which is correct:
http://answers.unity3d.com/questions/11963/how-to-create-multidimensional-arrays-in-javascrip.html http://answers.unity3d.com/questions/222522/multidimensional-array-in-javascript.html
So, using Unity 4.x, how would I create a multidimensional (specifically, 3D) array of classes?
As a bonus question, is it possible to create this 3D array as a dynamic array of some sort, so I can change the size dynamically?
EDIT: If I use the following code to create the array, how then would I set the size or add new elements?
var bricks:int[,,];
Answer by Tonyx97 · Aug 24, 2014 at 02:40 PM
The correct way to create a multiarray on JS is like this:
var multiArray:int[,,];
or if you have to define the length of the multiArray when scripts starts you can use:
private var multiArray:int[,,] = new int[10,10,10];
when says new int[10,10,10] means that you can put 10 3d integers, 10 like this 56,3,22.
Perfect! Thankyou. Only- If I do want to add elements later without defining the length immediately, how would I do that?
I have a little error on the last answer, it wasn't multiArray[10,10,10]
, the correct way is multiArray[10,10,10,10]
because in the index number 1 you have to put 3 variables more, so for defining is multiArray:int[,,,]
. You can use for example, to put a 3d coordinates you have to:
multiArray[0,0,0,1] = 1;
multiArray[0,0,1,0] = 5;
multiArray[0,1,0,0] = 3;
This means, that in the index 0 will be 3 coordinates.
Also - what? I'm not looking to store 3D coordinates like that, but rather access a 3D array by 3D coordinates. So, I would do this:
class thingclass
{
var randomdata:int;
}
var thing:thingclass[,,];
function Start()
{
thing[1,5,3].randomdata=42;
}
$$anonymous$$y problem is still how I would set the size of the array if I don't define it when I create it (because I don't think the above code is actually valid).
Your answer
Follow this Question
Related Questions
Multidimensional array variable size (JS) 1 Answer
How to Add Elements to Size-Undefined Multidimensional Array in Javascript? 0 Answers
How to Create Dynamic Multidimensional Array? 1 Answer
Physics.OverlapSphere with a min radius? 1 Answer
Newbie Problem with Multidimensional Arrays in JavaScript 0 Answers