- Home /
Can't access Array in a class instance multidimensional Array.
I have a Multidimensional Array
var cubes: VoxCube[,,];
I set it
function Awake () {
cubes = new VoxCube[l,w,h];
}
I then have a function to fill out all of the data in the VoxCube[,,]
function BaseChunk() {
for(var x=0; x < l; x++){
for(var y=0; y < w; y++){
for(var z=0; z < h; z++){
var isVisible: boolean = false;
var isMeshVect: int = 0; // 0 not used 1 used
var isGround: boolean = false;
if(y == groundOffset){
isGround = true;
isMeshVect = 1;
isVisible = true;
}
//init location
cubes[x,y,z] = new VoxCube();
var voxLoc: VoxCube = cubes[x,y,z] as VoxCube;
voxLoc.visible = isVisible;
voxLoc.origin = Vector3(x,y,z);
//setup ghost vertices
ERROR: null----> voxLoc.ghostVertices[0] = Vector4(x*s,y*s,z*s,isMeshVect); //0,0,0
VoxCube is this
class VoxCube extends Object
{
public var visible: boolean;
public var origin: Vector3; //point location in VoxLevel
//The basic locations of the 8 Verticies:
public var ghostVertices: Array;
public var textureIds: Array;//MAX 6 one cube for each side.
public var exportedVertices: Array;
public var exportedTriangles: Array;
public var exportedUV: Array;
}
It keeps telling me voxLoc at the first ghostVertices line is a Null Reference Exception. I don't get why voxLoc moments before is fine but as soon as I try to put anything into that array within the VoxCube class it barfs on me. I'm sure it's something simple I'm missing. Any advice is welcome.
Answer by Eric5h5 · Dec 28, 2012 at 04:53 AM
You need to initialize ghostVertices (and the other arrays) before you can use them. However you really should not use Array, which is slow and not type-safe. Use a built-in array, namely Vector4[], or a generic List of Vector 4, namely List.< Vector4 >, if it's supposed to be a dynamically-sized array. (You still need to initialize them though.)
Thanks. I should have realized that. Too much time staring at it I suppose :)