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;
}
}