Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Dec 31, 2014 at 07:13 AM by iamthecoolguy11 for the following reason:

Not getting a reply

avatar image
0
Question by iamthecoolguy11 · Dec 30, 2014 at 07:51 AM · c#meshvoxeltriangles

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. alt text

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; 
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iamthecoolguy11 · Dec 30, 2014 at 08:18 AM 0
Share

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

avatar image HarshadK · Dec 30, 2014 at 09:14 AM 1
Share

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.

avatar image iamthecoolguy11 · Dec 30, 2014 at 05:03 PM 0
Share

i dont know what you mean so I update the script in the question

avatar image HarshadK · Dec 31, 2014 at 07:26 AM 0
Share

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.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Marching cubes problem, some tris not drawing properly 2 Answers

How to generate a subdivided plane mesh 1 Answer

Multiple Cars not working 1 Answer

Getting meshes to render properly, and coloring triangles of a mesh 0 Answers

How do i create a Mesh in the middle of a Catmull spline? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges