Hi, it is me again. So I recieved this error with my code:
IndexOutOfRangeException: Index was outside the bounds of the array.
Chunk.CheckVoxel (UnityEngine.Vector3 pos) (at Assets/Scripts/C#/Chunk.cs:59)
Chunk.AddVoxelDataToChunk (UnityEngine.Vector3 pos) (at Assets/Scripts/C#/Chunk.cs:64)
Chunk.CreateMeshData () (at Assets/Scripts/C#/Chunk.cs:45)
Chunk.Start () (at Assets/Scripts/C#/Chunk.cs:22)
I have looked at my code here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chunk : MonoBehaviour
{
public MeshRenderer meshRenderer;
public MeshFilter meshFilter;
int vertexIndex = 0;
List<Vector3> vertices = new List<Vector3> ();
List<int> triangles = new List<int> ();
List<Vector2> uvs = new List<Vector2> ();
byte[,,] voxelMap = new byte[VoxelData.ChunkWidth, VoxelData.ChunkHeight, VoxelData.ChunkWidth];
World world;
void Start () {
world = GameObject.Find("World").GetComponent<World>();
PopulateVoxelMap ();
CreateMeshData ();
CreateMesh ();
}
void PopulateVoxelMap () {
for (int y = 0; y < VoxelData.ChunkHeight; y++) {
for (int x = 0; x < VoxelData.ChunkWidth; x++) {
for (int z = 0; z < VoxelData.ChunkWidth; z++) {
if (y < 1)
voxelMap[x, y, z] = 0;
else if (y == VoxelData.ChunkHeight - 1)
voxelMap[x, y, z] = 3;
else
voxelMap[x, y, z] = 1;
}
}
}
}
void CreateMeshData () {
for (int y = 0; y < VoxelData.ChunkHeight; y++) {
for (int x = 0; x < VoxelData.ChunkWidth; x++) {
for (int z = 0; z < VoxelData.ChunkWidth; z++) {
AddVoxelDataToChunk (new Vector3(x, y, z));
}
}
}
}
bool CheckVoxel (Vector3 pos) {
int x = Mathf.FloorToInt (pos.x);
int y = Mathf.FloorToInt (pos.y);
int z = Mathf.FloorToInt (pos.z);
if (x < 0 || x > VoxelData.ChunkWidth - 1 || y < 0 || y > VoxelData.ChunkHeight - 1 || z < 0 || z > VoxelData.ChunkWidth - 1) {
return false;
}
return world.blocktypes[voxelMap[x, y, z]].isSolid;
}
void AddVoxelDataToChunk (Vector3 pos) {
for (int p = 0; p < 6; p++) {
if(!CheckVoxel(pos + VoxelData.faceChecks[p])) {
byte blockID = voxelMap[(int)pos.x, (int)pos.y, (int)pos.z];
vertices.Add (pos + VoxelData.voxelVerts [VoxelData.voxelTris [p, 0]]);
vertices.Add (pos + VoxelData.voxelVerts [VoxelData.voxelTris [p, 1]]);
vertices.Add (pos + VoxelData.voxelVerts [VoxelData.voxelTris [p, 2]]);
vertices.Add (pos + VoxelData.voxelVerts [VoxelData.voxelTris [p, 3]]);
AddTexture(world.blocktypes[blockID].GetTextureID(p));
triangles.Add (vertexIndex);
triangles.Add (vertexIndex + 1);
triangles.Add (vertexIndex + 2);
triangles.Add (vertexIndex + 2);
triangles.Add (vertexIndex + 1);
triangles.Add (vertexIndex + 3);
vertexIndex += 4;
}
}
}
void CreateMesh () {
Mesh mesh = new Mesh ();
mesh.vertices = vertices.ToArray ();
mesh.triangles = triangles.ToArray ();
mesh.uv = uvs.ToArray ();
mesh.RecalculateNormals ();
meshFilter.mesh = mesh;
}
void AddTexture (int textureID) {
float y = textureID / VoxelData.TextureAtlasSizeInBlocks;
float x = textureID % VoxelData.TextureAtlasSizeInBlocks;
x *= VoxelData.NormalizedBlockTextureSize;
y *= VoxelData.NormalizedBlockTextureSize;
y = 1f - y - VoxelData.NormalizedBlockTextureSize;
uvs.Add (new Vector2(x, y));
uvs.Add (new Vector2(x, y + VoxelData.NormalizedBlockTextureSize));
uvs.Add (new Vector2(x + VoxelData.NormalizedBlockTextureSize, y));
uvs.Add (new Vector2(x + VoxelData.NormalizedBlockTextureSize, y + VoxelData.NormalizedBlockTextureSize));
}
}
And here is my voxel Data Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VoxelData
{
public static readonly int ChunkWidth = 5;
public static readonly int ChunkHeight = 15;
public static readonly int TextureAtlasSizeInBlocks = 4;
public static float NormalizedBlockTextureSize {
get { return 1f / (float)TextureAtlasSizeInBlocks; }
}
public static readonly Vector3[] voxelVerts = new Vector3[8] {
// Represents Location of Vertices in the Cube (Voxel)
new Vector3(0.0f, 0.0f, 0.0f),
new Vector3(1.0f, 0.0f, 0.0f),
new Vector3(1.0f, 1.0f, 0.0f),
new Vector3(0.0f, 1.0f, 0.0f),
new Vector3(0.0f, 0.0f, 1.0f),
new Vector3(1.0f, 0.0f, 1.0f),
new Vector3(1.0f, 1.0f, 1.0f),
new Vector3(0.0f, 1.0f, 1.0f),
};
public static readonly Vector3[] faceChecks = new Vector3[6] {
new Vector3(0.0f, 0.0f, -1.0f),
new Vector3(0.0f, 0.0f, 1.0f),
new Vector3(0.0f, 1.0f, 0.0f),
new Vector3(0.0f, -1.0f, 0.0f),
new Vector3(-1.0f, 0.0f, 0.0f),
new Vector3(1.0f, 0.0f, 0.0f),
};
public static readonly int[,] voxelTris = new int[6,4] {
// Represents the Voxel Triangle Faces
// 0 1 2 2 1 3
{0, 3, 1, 2}, // Back Face
{5, 6, 4, 7}, // Front Face
{3, 7, 2, 6}, // Top Face
{1, 5, 0, 4}, // Bottom Face
{4, 7, 0, 3}, // Left Face
{1, 2, 5, 6}, // Right Face
};
public static readonly Vector2[] voxelUvs = new Vector2[4] {
new Vector2 (0.0f, 0.0f),
new Vector2 (0.0f, 1.0f),
new Vector2 (1.0f, 0.0f),
new Vector2 (1.0f, 1.0f),
};
}
I was using a tutorial and referencing his scripts but it appears I have reached an issue. I am sure I missed something but I havenât been able to find where I messed up.
Here is where the YouTube Content Creators scripts are:
So did I miss something or is the problem elsewhere? I have one other script but I doubt it is in there.
Thanks ahead of time.