- Home /

# I don't know how to do multidimensional arrays

For some reason, unity wont let me create an 8 dimensional array like this:

```
var verlist : int[,,,,,,,] = new int[2,2,2,2,2,2,2,2];
```

but has no problem with a 4 dimensional array like this:

```
var verlist : int[,,,] = new int[2,2,2,2];
```

Its the exact same code, but unity sends the error message:

No appropriate version of 'boo.Lang.Builtins.matrix' for the argument list '(int, int, int, int, int, int, int, int)' was found

Does anyone have any idea why and how to fix this?

What on earth are you doing with an 8 dimensional array ???? :D

I'm guessing that's breaking a design limit...

for some reason 8 makes me think of using a look up table and binary, but you probably need more numbers than 1s and 0s so don't mind me...

Actually, I only need 1s and 0s. Im using the 8 dimensional array in a marching cubes algorithm Im designing.

@Monko I would like to think I know about cubes, dimensions, and how lucky the number 8 is in Chinese culture, but it's not immediately apparent to me why one would use sparse "8 dimensional" booleans in an algorithm related to these things.

Purely as a curiosity can you explain simply in a sentence how you use it in your algorithm??

Ok, im using an 8 dimensional array for the marching cubes algorithm. (if you dont know what that is, look at this website http://paulbourke.net/geometry/polygonise/)

The marching cube algorithm turns "on" and "off" the vertices in a 3d grid. Each cube in the 3d grid of vertices has 8 corners (like all cubes do). Because Im generating a unique set of triangles for each possible combination of corners turned on and off in each cube, i decided to create an 8th dimensional array to handle it all. (If that makes any sense at all)

**Answer** by whydoidoit
·
May 15, 2013 at 11:42 PM

Create a flat array and make a mapping function to it:

```
var eightDimensionalArray = new int[256];
function GetElement(x1:int,x2:int,x3:int,x4:int,x5:int,x6:int,x7:int,x8:int):int
{
return eightDimensionalArray[ ((x8 << 7) + (x7 << 6) + (x6 << 5) + (x5 << 4) + (x4 << 3) + (x3 << 2) + (x2 << 1) + x1)];
}
function SetElement(x1 : int, x2 : int, x3 : int, x4 : int, x5 : int, x6 : int, x7 : int, x8 : int, value: int)
{
print( (x8 << 7) + (x7 << 6) + (x6 << 5) + (x5 << 4) + (x4 << 3) + (x3 << 2) + (x2 << 1) + x1);
eightDimensionalArray[ ((x8 << 7) + (x7 << 6) + (x6 << 5) + (x5 << 4) + (x4 << 3) + (x3 << 2) + (x2 << 1) + x1)] = value;
}
SetElement(0,0,0,1,0,0,0,1,5); //example to "Set" a value in the array
print(GetElement(0,0,0,1,0,0,0,1)); //example to retrieve whatever you set. In this case, 5.
```

Wow! Thanks whydoidoit! That was very clever. This works perfectly for my game.

I do have to mention, I had to edit your code a little, so I posted the working version below:

```
var eightDimensionalArray = new int[256];
function GetElement(x1:int,x2:int,x3:int,x4:int,x5:int,x6:int,x7:int,x8:int):int
{
return eightDimensionalArray[ ((x8 << 7) + (x7 << 6) + (x6 << 5) + (x5 << 4) + (x4 << 3) + (x3 << 2) + (x2 << 1) + x1)];
}
function SetElement(x1 : int, x2 : int, x3 : int, x4 : int, x5 : int, x6 : int, x7 : int, x8 : int, value: int)
{
print( (x8 << 7) + (x7 << 6) + (x6 << 5) + (x5 << 4) + (x4 << 3) + (x3 << 2) + (x2 << 1) + x1);
eightDimensionalArray[ ((x8 << 7) + (x7 << 6) + (x6 << 5) + (x5 << 4) + (x4 << 3) + (x3 << 2) + (x2 << 1) + x1)] = value;
}
SetElement(0,0,0,1,0,0,0,1,5); //example to "Set" a value in the array
print(GetElement(0,0,0,1,0,0,0,1)); //example to retrieve whatever you set. In this case, 5.
```

Ah yep, brackets, scripting in the answer box again :S

**Answer** by amostajo
·
May 15, 2013 at 11:34 PM

Try using something different than arrays maybe?

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F

Perhaps a list will do for you.

You could create a class with the values you want, in this case since you are needing 2 ints, something like this will do:

```
class MyObject {
public var myIntA : int;
public var myIntB : int;
/**
Default constructor.
*/
public function MyObject () {
myIntA = myIntB = 0;
}
}
```

You then define your list as:

```
// Add this on top your script
import System.Collections.Generic;
// Here you create a list of your object, it supports more tham 8 I assure you
var verlist : List.<MyObject>;
function sampleUsage () {
verlist = new List.<MyObject>();
// Adding new object with default constructor.
verlist.Add(new MyObject());
// Adding new object with custom int values
var newitem : MyObject = new MyObject();
var newitem.myIntA = 5;
var newitem.myIntB = 10;
verlist.Add(newitem);
// foreach
foreach (var myobject : MyObject in verlist) {
// Displaying all values in console
Debug.Log(myobject.myIntA);
}
// Displaying value of MyObject added
Debug.Log(verlist[1].myIntB);
}
```

The only problem with that is that it's a list of *x* things, that's not an 8 dimensional structure if you add 8 things to the list.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.