I can't fix my "Cannot apply indexing with [ ] to an expression of type:..." error

Hi all,

I am trying to make a “Chunk” class which currently looks like this:

public class Chunk {
    public GridCell[][][] chunkGridCells;

    public Chunk(int chunkSize) {
        chunkGridCells = new GridCell[chunkSize][chunkSize][chunkSize];
    }
}

The class is supposed to be a holder for a 3d array of “GridCell” objects which look like this:

public class GridCell {
    public Vector3[] cubePoints;
    public double[] pointValues;
    public int[] gridSpaceCoord;
    public int cubeCellIndex;

    public GridCell(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, Vector3 p5, Vector3 p6, Vector3 p7, Vector3 p8, double v1, double v2, double v3, double v4, double v5, double v6, double v7, double v8, int gridSpaceX, int gridSpaceY, int gridSpaceZ) {
        cubePoints = new Vector3[8];
        cubePoints [0] = p1;
        cubePoints [1] = p2;
        cubePoints [2] = p3;
        cubePoints [3] = p4;
        cubePoints [4] = p5;
        cubePoints [5] = p6;
        cubePoints [6] = p7;
        cubePoints [7] = p8;

        pointValues = new double[8];
        pointValues [0] = v1;
        pointValues [1] = v2;
        pointValues [2] = v3;
        pointValues [4] = v5;
        pointValues [5] = v6;
        pointValues [6] = v7;
        pointValues [7] = v8;

        gridSpaceCoord = new int[3];
        gridSpaceCoord [0] = gridSpaceX;
        gridSpaceCoord [1] = gridSpaceY;
        gridSpaceCoord [2] = gridSpaceZ;

        cubeCellIndex = 0;
    }
}

The “Cannot apply indexing…” error is thrown at the line inside of the “Chunk” constructor. I can’t seem to find the source of the error, but I have a feeling that I am missing something fairly basic. Thank you for any help you can give.

Copied your code exactly Have no problems with the GridCell constructor, but DO have issue with the chunkGridCells initialiser.
You can’t do some3dArray = new type[×][×][×] because you cannot specify the sizes of the 2nd and 3rd dimension in one go for arrays of this type.
You will need to start with
some3dArray = new type[×][ ][ ]
then iterate through the first dimension and do
some3dArray *= new type[×][ ]*
then iterate through those new arrays and do
some3dArray*[j] = new type[×]*
Or just use type[,,] some3dArray = new type[x,x,x]

Thank you so much, that fixed it! I didnt know 3d arrays had to be initialized step by step like that, thanks you for the info :slight_smile:

https://wiki.unity3d.com/index.php/Choosing_the_right_collection_type
info on collections
You were using a ‘jagged array’, which are nested 1-d arrays

@hpjohn , while a multidimensional array is a lot easier to use, Unity recommends against it, see Special Optimizations. If you have to iterate over the values a lot, a jagged array is significiantly better than a multidimensional array.

@tommyboy611 , there might be something interesting for you, see Indexers (C# Programming Guide) by Microsoft. This even works for multidimensional access, with more than one dimension, see “c# indexer multi dimensional” in your favorite search engine.

Just so you know, GridCell[ ][ ][ ] is not a 3-dimensional array. It is a one-dimensional array of one-dimensional arrays of one-dimensional arrays. If you wanted a 3-dimensional array, the syntax is GridCell[,] like hpjohn wrote in his post.