# NullReferenceException (640485)

**URL:** https://discussions.unity.com/t/nullreferenceexception-640485/640485
**Category:** Scripting
**Tags:** Scripting
**Created:** [September 20, 2016, 6:25pm UTC](https://discussions.unity.com/t/nullreferenceexception-640485/640485 "2016-09-20T18:25:35Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![zollo\_hd](https://avatars.discourse-cdn.com/v4/letter/z/35a633/32.png) [@zollo\_hd](https://discussions.unity.com/u/zollo_hd)
#### Post date: [September 20, 2016, 6:25pm UTC](https://discussions.unity.com/t/nullreferenceexception-640485/640485/1 "2016-09-20T18:25:35Z")

</div>

I always get a NullReferenceException. I’m pretty new to Unity, so I don’t know a lot.

```csharp
using UnityEngine;
using System.Collections.Generic;
using Simplex;
using MarchingCubes;

public class VoxelDataGenerator : MonoBehaviour {

    MarchingCubes.MarchingCubes marchingCubes;
    public int seed;
    public float isolevel;
    public float voxelsize;
    public float noiseScale;
    Mesh mesh;
    MeshFilter meshFilter;
    List<Vector3> vertices;
    List<int> tris;

    // Use this for initialization
    void Start () {
        tris = new List<int>();
        vertices = new List<Vector3>();

        //Setting base variables
        voxelsize = 1;
        isolevel = 0;

        Vector3[] gridcellPoints = new Vector3[8];
        for (int i = 0; i < 8; i++)
            gridcellPoints[i] = new Vector3();

        //Defining classes
        float[] value = new float[8];
        for (int c = 0; c < 8; c++)
            value[c] = new float();

        GRIDCELL gridcell = new GRIDCELL(gridcellPoints, new float[8]);
        TRIANGLE[] triangles = new TRIANGLE[6];

        Vector3[] triangle = new Vector3[3];
        for (int x = 0; x < 3; x++)
            triangle[x] = new Vector3();

        for (int a = 0; a < 6; a++)
            triangles[a] = new TRIANGLE(triangle);

        marchingCubes = new MarchingCubes.MarchingCubes();

        //Defining the seed
        Noise noise = new Noise();
        seed = Noise.Seed;

        //How many Triangles were put out by Marching Cubes
        int triCount;

        mesh = new Mesh();
        meshFilter = GetComponent<MeshFilter>();

        for(int x = 0; x < 16; x++)
        {for (int z = 0; z < 16; z++)
        {for (int y = 0; y < 8; y++)
                {
                    //Put location information in our Gridcell
                    gridcell.p[0] = new Vector3(x - voxelsize / 2, y - voxelsize / 2, z - voxelsize / 2);
                    gridcell.p[1] = new Vector3(x + voxelsize / 2, y - voxelsize / 2, z - voxelsize / 2);
                    gridcell.p[2] = new Vector3(x + voxelsize / 2, y - voxelsize / 2, z + voxelsize / 2);
                    gridcell.p[3] = new Vector3(x - voxelsize / 2, y - voxelsize / 2, z + voxelsize / 2);
                    gridcell.p[4] = new Vector3(x - voxelsize / 2, y + voxelsize / 2, z - voxelsize / 2);
                    gridcell.p[5] = new Vector3(x + voxelsize / 2, y + voxelsize / 2, z - voxelsize / 2);
                    gridcell.p[6] = new Vector3(x + voxelsize / 2, y + voxelsize / 2, z + voxelsize / 2);
                    gridcell.p[7] = new Vector3(x - voxelsize / 2, y + voxelsize / 2, z + voxelsize / 2);

                    //Put Noise Scalar Values in the Gridcell
                    gridcell.val[0] = noise.CalcPixel3D((int)gridcell.p[0].x, (int)gridcell.p[0].y, (int)gridcell.p[0].z, noiseScale);
                    gridcell.val[1] = noise.CalcPixel3D((int)gridcell.p[1].x, (int)gridcell.p[1].y, (int)gridcell.p[1].z, noiseScale);
                    gridcell.val[2] = noise.CalcPixel3D((int)gridcell.p[2].x, (int)gridcell.p[2].y, (int)gridcell.p[2].z, noiseScale);
                    gridcell.val[3] = noise.CalcPixel3D((int)gridcell.p[3].x, (int)gridcell.p[3].y, (int)gridcell.p[3].z, noiseScale);
                    gridcell.val[4] = noise.CalcPixel3D((int)gridcell.p[4].x, (int)gridcell.p[4].y, (int)gridcell.p[4].z, noiseScale);
                    gridcell.val[5] = noise.CalcPixel3D((int)gridcell.p[5].x, (int)gridcell.p[5].y, (int)gridcell.p[5].z, noiseScale);
                    gridcell.val[6] = noise.CalcPixel3D((int)gridcell.p[6].x, (int)gridcell.p[6].y, (int)gridcell.p[6].z, noiseScale);
                    gridcell.val[7] = noise.CalcPixel3D((int)gridcell.p[7].x, (int)gridcell.p[7].y, (int)gridcell.p[7].z, noiseScale);

                    //Run Marching and put number od tris in variable
                    triCount = marchingCubes.Polygonise(gridcell, isolevel, triangles);

                    for(int i = 0; i < triCount; i = i + 3)
                    {
                        vertices.Add(triangles[i].p[0]);
                        vertices.Add(triangles[i + 1].p[1]);
                        vertices.Add(triangles[i + 2].p[2]);
                        tris.Add(vertices.Count - 2);
                        tris.Add(vertices.Count - 1);
                        tris.Add(vertices.Count);
                    }
        }
        }
        }

        mesh.RecalculateNormals();
        meshFilter.mesh = mesh;

        gameObject.AddComponent<MeshCollider>();
    }
   
    // Update is called once per frame
    void Update () {
   
    }
}

```

---

<div class="post-metadata">

### Author: ![Brathnann](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@Brathnann](https://discussions.unity.com/u/Brathnann)
#### Post date: [September 20, 2016, 7:05pm UTC](https://discussions.unity.com/t/nullreferenceexception-640485/640485/2 "2016-09-20T19:05:59Z")

</div>

You’ll need to provide more info. What line is the exception pointing to?

---

<div class="post-metadata">

### Author: ![Timelog](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/timelog/32/307301_2.png) [@Timelog](https://discussions.unity.com/u/Timelog)
#### Post date: [September 20, 2016, 7:12pm UTC](https://discussions.unity.com/t/nullreferenceexception-640485/640485/3 "2016-09-20T19:12:38Z")

</div>

Can you post the full error you get? This way it’s near impossible for us to see where the error is thrown while it’s clearly stated in the error message.

---

<div class="post-metadata">

### Author: ![zollo\_hd](https://avatars.discourse-cdn.com/v4/letter/z/35a633/32.png) [@zollo\_hd](https://discussions.unity.com/u/zollo_hd)
#### Post date: [September 21, 2016, 12:23pm UTC](https://discussions.unity.com/t/nullreferenceexception-640485/640485/4 "2016-09-21T12:23:10Z")

</div>

Here’s the error code:  
NullReferenceException: Object reference not set to an instance of an object  
VoxelDataGenerator.Start () (at Assets/VoxelDataGenerator.cs:63)

I also want to say, that I tried a lot of things, to fix the error, so the code might contain a lot things, which wouldn’t be needed.

---

<div class="post-metadata">

### Author: ![Baste](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/baste/32/880289_2.png) [@Baste](https://discussions.unity.com/u/Baste)
#### Post date: [September 21, 2016, 12:42pm UTC](https://discussions.unity.com/t/nullreferenceexception-640485/640485/5 "2016-09-21T12:42:01Z")

</div>

Gridcell.p is null. Did you forget to initialize it in the constructor?

An alternative is that the class GRIDCELL is a MonoBehaviour, in which case Unity should complain that you can’t use constructors. If that’s the case, just turn GRIDCELL into a normal class.

And holy hell don’t write class names that’s all caps.

---

<div class="post-metadata">

### Author: ![Baste](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/baste/32/880289_2.png) [@Baste](https://discussions.unity.com/u/Baste)
#### Post date: [September 21, 2016, 12:43pm UTC](https://discussions.unity.com/t/nullreferenceexception-640485/640485/6 "2016-09-21T12:43:11Z")

</div>

By the way, when an error message says “At Assets/VoxelDataGenerator.cs:63”, it’s because the error is on line 63. If you double-click the error, the script should also be opened on that line.

---

<div class="post-metadata">

### Author: ![zollo\_hd](https://avatars.discourse-cdn.com/v4/letter/z/35a633/32.png) [@zollo\_hd](https://discussions.unity.com/u/zollo_hd)
#### Post date: [September 21, 2016, 1:04pm UTC](https://discussions.unity.com/t/nullreferenceexception-640485/640485/7 "2016-09-21T13:04:14Z")

</div>

Thank you, It was the constructor, now it’s working. I just converted the code from C++ to C# and in the C++ code the class name was caps, usually I don’t do that.
