- Home /
Not getting a reply
How come do I keep getting this error on my voxel script
I made this voxel script from scratch and i'm still new to voxel but I made this on my own so im gussing its just a calculation isshu but iv tried a lot of different things and its still not working correctly it just gets this error on play.
here is the script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshCollider))]
public class RealVoxel : MonoBehaviour {
public int length = 1;
public int width = 1;
public MeshRenderer meshRender;
public MeshFilter meshFilter;
public MeshCollider meshCollider;
public List<int>tris = new List<int>();
public List<Vector2>uvs = new List<Vector2>();
public List<Vector3>verts = new List<Vector3>();
public int block = 0;
public int tri = 0;
void Start()
{
meshRender = this.GetComponent<MeshRenderer>();
meshFilter = this.GetComponent<MeshFilter>();
meshCollider = this.GetComponent<MeshCollider>();
genCube();
EditTerrain();
}
void EditTerrain()
{
Mesh mesh= new Mesh();
Vector3 pos = new Vector3(0,1,1);
for(int i = 0; i < verts.Count; i++)
{
if(verts[i] == pos)
{
block = i;
break;
}
}
tri = block * 6;
verts.Remove(verts[block]);
verts.Remove(verts[block + 1]);
verts.Remove(verts[block + 2]);
verts.Remove(verts[block + 3]);
uvs.Remove(uvs[block]);
uvs.Remove(uvs[block+1]);
uvs.Remove(uvs[block+2]);
uvs.Remove(uvs[block+3]);
tris.Remove(tris[tri]);
tris.Remove(tris[tri+1]);
tris.Remove(tris[tri+2]);
tris.Remove(tris[tri+3]);
tris.Remove(tris[tri+4]);
tris.Remove(tris[tri+5]);
Recalcutlate(mesh);
}
void genCube()
{
for(int x = 0; x <= length; x++)
{
top(new Vector3(x,1,0));
for(int z = 0; z <= width; z++)
{
top(new Vector3(x,1,z));
}
}
}
public virtual void removeBlock(Vector3 corner)
{
Mesh visualMesh = new Mesh();
}
public virtual void top(Vector3 corner)
{
Mesh visualMesh = new Mesh();
int index = verts.Count;
verts.Add(corner);
verts.Add(corner + new Vector3(0,0,1));
verts.Add(corner + new Vector3(1,0,0) + new Vector3(0,0,1));
verts.Add(corner + new Vector3(1,0,0));
uvs.Add(new Vector2(0,0));
uvs.Add(new Vector2(0,1));
uvs.Add(new Vector2(1,1));
uvs.Add(new Vector2(1,0));
tris.Add(index + 0);
tris.Add(index + 1);
tris.Add(index + 2);
tris.Add(index + 2);
tris.Add(index + 3);
tris.Add(index + 0);
Recalcutlate(visualMesh);
}
void Recalcutlate(Mesh visualMesh)
{
visualMesh.vertices = verts.ToArray();
visualMesh.uv = uvs.ToArray();
visualMesh.triangles = tris.ToArray();
visualMesh.RecalculateBounds();
visualMesh.RecalculateNormals();
meshFilter.mesh = visualMesh;
meshCollider.sharedMesh = visualMesh;
}
}
Answer by HarshadK · Dec 30, 2014 at 08:12 AM
Your block is 0 and tris is zero at the start then you are performing the action:
tri = block * 6;
which yields 0 since 0 * 6 = 0.
Now you are accessing tris with:
tris.Remove(tris[tri-1]);
which is similar to: tris.Remove(tris[0-1]) = tris.Remove(tris[-1]).
And there is no vertex present at -1 index of the tris list.
ok so I changed the for loop to this
for(int x = 0; x <= length; x++)
{
top(new Vector3(x,1,0));
for(int z = 0; z <= width; z++)
{
top(new Vector3(x,1,z));
}
}
and the tris to this
uvs.Remove(uvs[block]);
uvs.Remove(uvs[block+1]);
uvs.Remove(uvs[block+2]);
uvs.Remove(uvs[block+3]);
tris.Remove(tris[tri]);
tris.Remove(tris[tri+1]);
tris.Remove(tris[tri+2]);
tris.Remove(tris[tri+3]);
tris.Remove(tris[tri+4]);
tris.Remove(tris[tri+5]);
but im still getting the same error
What are the values inside your tris and verts Lists? Since you might still be referencing to a vertex that is not available for triangle to access.
i dont know what you mean so I update the script in the question
If you just Debug your values for your verts and tris list after deleting verts and tris you will realize that your verts list has 20 vertices in the list but your tris list points to vertices 21, 22, 23, etc which are not present in your verts list. These are your vertices out of bound. After you remove a specific vertices from list you also have to update your tris list with values that will reflect the new vertices list and not just remove the tris related to the vertices deleted.