Search Unity

Multi-dimensional arrays

Discussion in 'Scripting' started by Samantha, Oct 22, 2005.

  1. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    I'm trying to declare a 3 dimensional array in C#, and I'm having some errors thrown at me. This line is the culprit:

    Code (csharp):
    1. public int[] 3dArray = new int[ , , ];
    this gives me the error "new expression required () or [] after type. So if I change it to:

    Code (csharp):
    1. public int[] 3dArray = new int[3];
    and I get the error "incorrect number of indexes for array. expected 1 got 3"

    What's the correct way to declare multidimensional arrays in C# (and JS while I'm at it)?
     
    lordelmsworth likes this.
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  3. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    thanks Joachim, it works if I explicitly define my array's dimensions, but what if I want my array to have a dynamic size based on the variables in the script? When working in C# in windows apps, the declaration

    Code (csharp):
    1. int[ , , ] myArray = new int[]
    works just fine, but Unity yells at me. Am I forced to declare my array's dimensions when I instantiate it?
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You can of course use ArrayList and embed them inside each other.

    Not sure why your example compiles on windows, but as far as i know the C# specification requires you to declare the array dimension.
    Of course csc could have more relaxed behaviour than in the spec or i might be wrong and mono's C# compiler has a bug.
     
  5. jlchavez

    jlchavez

    Joined:
    Oct 25, 2005
    Posts:
    1
    On C# you can do this for a 3 dimensional array:

    Code (csharp):
    1. int[,,] myArray = new int[,,]{}
    the part of the {} is the initializer, giving the initial values, and if you have to set the values it could be something like this:

    Code (csharp):
    1. int[ , , ] myArray = new int[]{{{1,2},{3,4},{5,6}},{{7,8},{9,10},{11,12}},{{13,14},{15,16},{17,18}}
    This type of arrays are fixed arrays, you would have to have arrays defined as items on other arrays:

    Code (csharp):
    1. ArrayList firstDimension = new ArrayList();
    2. ArrayList secondDimensionChild = new ArrayList();
    3. ArrayList thirdDimensionChild = new ArrayList();
    4. firstDimension.Add(secondDimensionChild);
    5. secondDimensionChild.Add(thirdDimensionChild);
    6. thirdDimensionChild.Add(3);
    Then yo can access them through the ArraList's indexer (indexing 0 based):

    Code (csharp):
    1. int value = (int)firstDimension[0][0][0];
     
  6. Theotherones

    Theotherones

    Joined:
    Feb 27, 2022
    Posts:
    1
    I'm not sure if this is the most efficient way or if this has some artifacts that I haven't noticed, but what I found is that Unity doesn't mind declaring multiple dimension arrays

    Code (CSharp):
    1. void initDungeonPoints(){
    2.         voxelPoints = new float[xCount][][];//sets up the array with an x size of [xCount]
    3.         // loops through the 3D array and sets the value for the first dimension to
    4.         // a 2d array with the "x" dimension that is acting as the 3D array's
    5.         // y set to the y size of [ySize]
    6.         for (int x = xCount-1; x>=0; x--){
    7.             voxelPoints[x] = new float[yCount][];
    8.             // loops through the 2D array and sets the value of the first dimension of the
    9.             // 2D array to a 1D array of size [zCount]
    10.             for (int y = yCount-1; y>=0; y--){
    11.                 voxelPoints[x][y] = new float[zCount];
    12.                 //loops through the final array and sets all of the points to a float value of 1
    13.                 for (int z = zCount-1; z>=0; z--){
    14.                     voxelPoints[x][y][z] = 1f;
    15.                 }
    16.             }
    17.         }
    18.     }
    This might not work in previous versions of Unity, but it seems stable enough for my purposes.
     
  7. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    What you have there is not a multi-dimensional array. It is a one-dimensional array of one-dimensional arrays of one-dimensional arrays. You could still use it to store the same data, but It's not exactly the same thing.