# Null Reference Exception (564606)

**URL:** https://discussions.unity.com/t/null-reference-exception-564606/564606
**Category:** Scripting
**Tags:** Scripting
**Created:** [January 23, 2015, 4:42pm UTC](https://discussions.unity.com/t/null-reference-exception-564606/564606 "2015-01-23T16:42:18Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![maroltl](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@maroltl](https://discussions.unity.com/u/maroltl)
#### Post date: [January 23, 2015, 4:42pm UTC](https://discussions.unity.com/t/null-reference-exception-564606/564606/1 "2015-01-23T16:42:18Z")

</div>

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:

```javascript
#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:

```javascript
 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.

---

<div class="post-metadata">

### Author: ![KelsoMRK](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kelsomrk/32/205687_2.png) [@KelsoMRK](https://discussions.unity.com/u/KelsoMRK)
#### Post date: [January 23, 2015, 5:17pm UTC](https://discussions.unity.com/t/null-reference-exception-564606/564606/2 "2015-01-23T17:17:20Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![maroltl](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@maroltl](https://discussions.unity.com/u/maroltl)
#### Post date: [January 23, 2015, 5:56pm UTC](https://discussions.unity.com/t/null-reference-exception-564606/564606/3 "2015-01-23T17:56:37Z")

</div>

You were completely right. I placed GenerateMesh in Update and it works perfectly 🙂  
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?

---

<div class="post-metadata">

### Author: ![KelsoMRK](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kelsomrk/32/205687_2.png) [@KelsoMRK](https://discussions.unity.com/u/KelsoMRK)
#### Post date: [January 23, 2015, 10:36pm UTC](https://discussions.unity.com/t/null-reference-exception-564606/564606/4 "2015-01-23T22:36:37Z")

</div>

> **[Unity - Manual: Script Execution Order settings](https://docs.unity3d.com/Manual/class-MonoManager.html)**

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

---

<div class="post-metadata">

### Author: ![maroltl](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@maroltl](https://discussions.unity.com/u/maroltl)
#### Post date: [January 27, 2015, 5:22pm UTC](https://discussions.unity.com/t/null-reference-exception-564606/564606/5 "2015-01-27T17:22:15Z")

</div>

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