- Home /
How do I calculate this voxel script so I don't get a out of range error?
I made this voxel script from scratch and i'm still new to voxel but I made this on my own so im guessing 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. 
This is my code
 sing 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 iamthecoolguy11 · Dec 31, 2014 at 07:42 AM
ok so I figured it out I was trying to remove things from the list instead of changing them so all I need to do is change this
 verts[block] = new Vector3(0,0,0); 
         verts[block+1] = new Vector3(0,0,0); 
         verts[block+2] = new Vector3(0,0,0); 
         verts[block+3] = new Vector3(0,0,0); 
 
         uvs[block] = new Vector3(0,0,0);
         uvs[block+1] = new Vector3(0,0,0);
         uvs[block+2] = new Vector3(0,0,0);
         uvs[block+3] = new Vector3(0,0,0);
 
         tris[tri] = 0; 
         tris[tri+1] = 0; 
         tris[tri+2] = 0; 
 
         tris[tri+3] = 0; 
         tris[tri+4] = 0; 
         tris[tri+5] = 0; 
Answer by Eluate · Dec 31, 2014 at 07:24 AM
Replace:
    for(int i = 0; i < verts.Count; i++)
          {
              if(verts[i] == pos)
              {
                  block = i; 
                  break; 
              }
          }
with
    for(int i = 0; i < verts.Count - 1; i++)
          {
              if(verts[i] == pos)
              {
                  block = i; 
                  break; 
              }
          }
Remember, arrays start with an index of 0. Count counts them and starts with 1 instead of zero.
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How come do I keep getting this error on my voxel script 1 Answer
Collider Performance Question 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                