Null Reference Exception (564606)

When I run my game in editor I get this error

NullReferenceException: Object reference not set to an instance of an object (wrapper managed-to-managed) object:ElementAddr (object,int,int,int)

It is an error in this javascript script:

#pragma strict
public var data : byte[,,];
public var worldX : int = 16;
public var worldY : int = 16;
public var worldZ : int = 16;
function Start () {
    
     data = new byte[worldX, worldY, worldZ];   //an error is made for this line
    
     for(var x : int = 0; x < worldX; x++){
         for(var y : int = 0; y < worldY; y++){
             for(var z : int = 0; z < worldZ; z++){
                
                 if(y <= 8){
                     data[x,y,z] = 1;
                 }
                
             }
         }
     }
}
function Block(x : int, y : int, z : int) : byte{
     if( x >= worldX || x < 0 || y >= worldY || y < 0 || z >= worldZ || z<0){
         return 1;   
     }
     return data[x,y,z];
    
}

but I found out that an error is acquired only if I run function GenerateMesh in other script where I use function Block from first script. This is the part of script where I use Block function:

 function GenerateMesh() {
     for(var x : int = 0; x < chunkSize; x++){
         for(var y : int = 0; y < chunkSize; y++){
             for(var z : int = 0; z < chunkSize; z++){
            
                 if(world.Block(x, y, z) != 0){
                
                     if(world.Block(x, y + 1, z) == 0){
                         CubeTop(x, y, z, world.Block(x, y, z));
                     }
                     if(world.Block(x, y - 1, z) == 0){
                         CubeBot(x, y, z, world.Block(x, y, z));
                     }
                     if(world.Block(x + 1, y, z) == 0){
                         CubeEast(x, y, z, world.Block(x, y, z));
                     }
                     if(world.Block(x - 1, y, z) == 0){
                         CubeWest(x, y, z, world.Block(x, y, z));
                     }
                     if(world.Block(x, y, z + 1) == 0){
                         CubeNorth(x, y, z, world.Block(x, y, z));
                     }
                     if(world.Block(x, y, z - 1) == 0){
                         CubeSouth(x, y, z, world.Block(x, y, z));
                     }
                 }
             }
         }
     }   
}

I just cant figure out what wrong. Any help is appreciated.

My guess is you’re calling GenerateMesh from the Start method and the object with that script on it is being initialized before the object with the Block script on it, therefore data is null.

You were completely right. I placed GenerateMesh in Update and it works perfectly :slight_smile:
Since this is 3d Voxel generating terrain I think this should not be called every frame. Any ideas of how would I approach this if I cant have it in Start function?

Or null check data and populate if it’s null instead of doing it in Start

Thank you. I used Script Execution since it is faster and easier to do. I had no idea that this thing exist.