Declaring a Bi-directional array as a built in array, is it possible?

Hi all,

I am having troubles converting my 2-dimensional array to a built-in type of array, mainly because I don’t know how to declare it :face_with_spiral_eyes:.
In case this wasn’t clear, here is an example:

if I want to declare a built-in array of integers or vector3’s I do:

var intArr : int[] : new int[5];
var vector3Arr : Vector3[] : new Vector3[5];

and that is fine, but what do I do when I have an array like this:

var multiArr = new Array();
multiarr = ([Vector2, Vector2, Vector2])

I tried using [ ][ ] but unity didn’t recognize it. Please help :slight_smile: Thanks! :wink:

You wouldn’t want to use Array. Use multidimensional built-in arrays, such as

var int2DArray = new int[5, 5];

–Eric

Hi Eric, thanks for a quick reply. But I am not sure how to use multidimensional built in arrays, can you pssibly give me an example that would use Vector2’s? so that I can more clearly understand, since [5,5] can be confusing when I don’t know which is which :slight_smile:

so how would I declare a multidimensional array that contains 4 vector 2’s?

Thanks!

I’m not sure what this means, since if you have 4 Vector2s, you only need a single-dimensional array, or possibly a 2D array which is 2 across and 2 down. How about this instead:

var v2Array = new Vector2[3, 4];

That makes a 2D array of Vector2s, which is 3 across and 4 down, for a total of 12. So you can access them like

v2Array[0, 1] = Vector2.one;
v2Array[2, 3] = Vector2(1.5, 1.5);

–Eric

edit: ninja’d.

ah great, thanks :slight_smile: that’s what I was looking for :slight_smile: cheers

Hi again,

I still am having problems with this issue. I guess I wasn’t clear enough originally so I will try to explain better now what exactly I need.

I need to be able to create and populate a twodimensional array whose each element is an array of Vector 2’s, so that I can declare it’s elements like this:

v2array[0,1] = ([Vector2.one, Vector2.zero, Vector2(1.5,1.5]);

I am sorry that I wasn’t clear enough in my original post, I am fairly new to programming so I am trying not to be lost in multidimensional arrays :smile:

I have tried declaring the array as:

var 2dArray : Vector2[,];

but logically that doesn’t work because what that expects is to have a single Vector2 in each of the elements, not an array of Vector2’s… I hope this is a bit clearer?

Thanks in advance!

A 2D Vector2 array is like a grid, or spreadsheet, where each cell contains a Vector2. If each row contains the same number of Vector2s, then that’s what you’d use. If each row contains a different number of Vector2s, then you can use a jagged array instead:

var 2dArray = [ [Vector2(1,1), Vector2(2,2)], [Vector2(3,3), Vector2(4,4), Vector2(5,5)] ];
print (2dArray[1][2]);

Note that declaring the type directly in JS currently isn’t implemented, so this results in an error:

var 2dArray : Vector2[][];

For a workaround, see here.

–Eric

Thanks! Now I got my mind about it and I think I know how to use a 2D array for what I need :slight_smile: Thanks again!