need help initializing custom multidimensional array

i would like to do a 3D array of a class rather than an integer. what i’d like to have is something like:

public CellData[,,] constellationInstance = new CellData[8,8,8];  
 
public class CellData
    {
        //define a Game Object to be associated with at Start
        public GameObject cellObject;
        //is the cell enabled? (true/false)
        public bool cellEnabled=false;
        //define an Intensity value (same value for all rows at a position)
        public int cellIntensity;
    }

but it seems the issue is that i can’t seem to set values because nothing’s been initialized in advance. checking the manual assignment examples on MSs site doesn’t seem to be helpful. using for loops seems like the way to go like so:

	for (int i = 0; i < 8; ++i)
            for (int j = 0; j < 8; ++j)
                for (int k = 0; k < 8; ++k){
					constellationInstance[k,j,i].cellEnabled=false;

					if(constellationInstance[k,j,i].cellIntensity == null)
						constellationInstance[k,j,i].cellIntensity=100;
					if(constellationInstance[k,j,i].cellObject == null)
					{	
                		GameObject cellObj = GameObject.Find("Constellation/Layer_"+k+"/Row_"+rowNames[j]+"/pos"+i);
	 					cellObj = cellObj.transform.GetChild(0).transform.gameObject;
						constellationInstance[k,j,i].cellObject = cellObj;
					}

}

running this i get an immediate NullRef, assumedly because i did not initialize the value properly. any clue how i can properly initialize this data?

When you initialize the array, you only reserve memory for 888 objects of class CellData, but you don’t create the objects yet, so the array is initially filled with nulls if you will.

What you want to in you for loop is:

for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
        for (int k = 0; k < 8; k++) {
            constellationInstance[k,j,i] = new CellData();
        }
    }
}

Now your array is ready to use the way you want to. E.G. setting cellEnabled to false everywhere, cellIntensity to 100 everywhere or whatever you want.

Also note: You have this line in your code:

constellationInstance[k,j,i].cellIntensity == null

And CellData.cellIntensity is an int - ints are non-nullable types, so can never be null.

excellent - that did the trick. thanks much!