NullReferenceException (640485)

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

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 () {
   
    }
}

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

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.

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.

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.

1 Like

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.

1 Like

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.