-1

so i have a 3 dimensional int array for my voxel game, and for debugging purposes i set all the blocks in the chunk to id 1(dirt) but on line 18 it gives the error OBJECT REFERENCE NOT SET TO AN INSTANCE OF AN OBJECT. and i know what this means, that the initial value has not been set. i just cant figure out WHY it would return this because i AM setting the initial value.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Chunk : MonoBehaviour
{
Mesh mesh = new Mesh();
int[,,] chunkData = new int[4, 64, 4];
// Start is called before the first frame update
void Start()
{
    for (int x = 0; x < 4; x++)
    {
        for (int y = 0; y < 64; y++)
        {
            for (int z = 0; z < 4; z++)
            {
                chunkData[x, y, z] = 1;// returns error here-----------------------------------------------------------------------------------------------------------------------------------
            }
        }
    }
    UpdateChunk();
}

// Update is called once per frame
void Update()
{
    
}
void UpdateChunk()
{
    List<Vector3> vertlist = new List<Vector3>();
    List<int> tris = new List<int>();
    int currentvert = 0;
    for (int x = 0; x < 4; x++)
    {
        for (int y = 0; y < 64; y++)
        {
            for (int z = 0; z < 4; z++)
            {
                Vector3[] adjacentBlocks = Block.GetAdjacentChunkBlocks(new Vector3(x, y, z));
                for (int i = 0; i < adjacentBlocks.Length; i++)
                {
                    Debug.Log(adjacentBlocks[i]);

                }
                
                

                foreach(Vector3 other in adjacentBlocks)
                {
                    Vector3 result = other - new Vector3(x, y, z);
                    if (result == Vector3.up && chunkData[(int)other.x, (int)other.y, (int)other.z] == 0 && chunkData[x, y, z] != 0)
                    {
                        Debug.Log("upface open");
                        vertlist.Add(new Vector3(x, y + 1, z));
                        vertlist.Add(new Vector3(x + 1, y + 1, z));
                        vertlist.Add(new Vector3(x, y + 1, z + 1));
                        vertlist.Add(new Vector3(x + 1, y + 1, z + 1));
                        tris.Add(2 + currentvert);
                        tris.Add(3 + currentvert);
                        tris.Add(0 + currentvert);
                        tris.Add(0 + currentvert);
                        tris.Add(3 + currentvert);
                        tris.Add(1 + currentvert); 

                    }
                    currentvert += 4;
                }
                Debug.Log("----------------------------------------------------------");
            }
        }
    }
    mesh.vertices = vertlist.ToArray();
    mesh.triangles = tris.ToArray();
    mesh.RecalculateNormals();
    GetComponent<MeshFilter>().mesh = mesh;
}

}
  • I would try doing a clean build. – Leo Bartkus Apr 25 '21 at 04:29
  • `Start` looks OK, I believe that some other piece of code is setting `chunkData` to null. – tymtam Apr 25 '21 at 04:30
  • BTW. Are you sure it's all in CAPS? ;) – tymtam Apr 25 '21 at 04:34
  • _"i just cant figure out WHY it would return this because i AM setting the initial value"_ -- no, you're not. Not before the code that needs it executes. See duplicate for extensive advice on how to debug this exception. – Peter Duniho Apr 25 '21 at 05:46
  • what do you mean? i called updatechunk after i set all the values to 1. and its returning the error on the line that also sets the value of it, so it wouldnt need to get a reference, right? – vainstains Apr 25 '21 at 18:05

1 Answers1

0

If Start is called when chunkData is new int[4, 64, 4]; it will work.

I suspect some other piece of code is setting chunkData to null.

Make chunkData readonly and the culprit will reveal itself.

tymtam
  • 31,798
  • 8
  • 86
  • 126