Question by
jayakvv · Jul 19, 2020 at 02:03 PM ·
c#unityeditorminecraft
IndexOutOfRangeException: Index was outside the bounds of the array. (wrapper managed-to-managed) System.Object.ElementAddr_1(object,int,int,int)
I am trying to make minecraft myself using unity and I ran into this error and I can't seem to figure out where this problem is.
My program is drawing new Minecraft chunks and making sure any unnecessary sides of the chunks are added. The line where the problem exists is here:
return world.blocktypes[voxelMap [x,y,z]].isSolid;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chunk
{
public ChunkCoord coord;
GameObject chunkObject;
MeshRenderer meshrenderer;
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;
public Chunk (ChunkCoord _coord, World _world){
world = _world;
coord = _coord;
chunkObject = new GameObject();
meshfilter = chunkObject.AddComponent<MeshFilter> ();
meshrenderer = chunkObject.AddComponent<MeshRenderer> ();
meshrenderer.material = world.material;
chunkObject.transform.SetParent(world.transform);
chunkObject.transform.position = new Vector3(coord.x * VoxelData.ChunkWidth,0f, coord.z * VoxelData.ChunkWidth);
chunkObject.name = "Chunk"+coord.x+","+coord.z;
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++){
voxelMap[x,y,z] = world.GetVoxel(new Vector3(x,y,z) + position);
}
}
}
}
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));
}
}
}
}
public bool isActive {
get { return chunkObject.activeSelf;}
set { chunkObject.SetActive(value);}
}
public Vector3 position {
get { return chunkObject.transform.position; }
}
bool IsVoxelInChunk (int x, int y, int z) {
if (x < 0 || x > VoxelData.ChunkWidth -1 || y < 0 || y > VoxelData.ChunkHeight -1|| z<0|| z>VoxelData.ChunkWidth - 1)
return false;
else
return true;
}
bool CheckVoxel (Vector3 pos){
int x = Mathf.FloorToInt(pos.x);
int y = Mathf.FloorToInt(pos.y);
int z = Mathf.FloorToInt(pos.z);
if (IsVoxelInChunk(x,y,z))
return world.blocktypes[world.GetVoxel(pos+position)].isSolid;
else
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 - (y * 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));
}
}
How do I solve this? Thank you!
Comment