Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

Failed Setting triangles. No idea why

$
0
0
I've followed quill18's tutorial on tilemaps on youtube. I've made some slight adjusments and it now throws me a Failed Setting triangles error. using UnityEngine; using System.Collections; [ExecuteInEditMode] [RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshRenderer))] [RequireComponent(typeof(MeshCollider))] public class TileMapGenerator : MonoBehaviour { public Material TileMapTex; public int sizex; public int sizez; public float tilesize; // Use this for initialization void Start () { BuildTileMesh(sizex, sizez, tilesize, TileMapTex); } /// /// Builds TileMap Mesh based on the numbers given' /// public void BuildTileMesh(int x_size, int z_size, float tileSize, Material material) { //Calculate nubmer of Verts, Tiles and Tris used for mesh generation int VertCountX = x_size + 1; int VertCountZ = z_size + 1; int VertCountTotal = VertCountX*VertCountZ; int TileCountTotal = VertCountX * VertCountZ; int TriCountTotal = TileCountTotal * 2; //Make the Mesh data arrays used for storing the meshes values Vector3[] verts = new Vector3[VertCountTotal]; Vector3[] norm = new Vector3[VertCountTotal]; Vector2[] uv = new Vector2[VertCountTotal]; int[] tris = new int[TriCountTotal * 3]; Debug.Log("Verts: " + verts.Length); Debug.Log("Tris: " + tris.Length); int x, z; //Generate Data for verts,norm and UV for (z = 0; z < VertCountZ; z++) { for (x = 0; x < VertCountX; x++) { verts[z*VertCountX+x] = new Vector3(x*tileSize,0,z*tileSize); norm[z*VertCountX + x] = Vector3.up; uv[z*VertCountX+x] = new Vector2((float)x/VertCountX,(float)z/VertCountZ); } } //Generate Tris for (z = 0; z < VertCountZ; z++) { for (x = 0; x < VertCountX; x++) { int tileIndex = z * x_size + x; int triOffset = tileIndex*6; //First Triangle tris[triOffset + 0] = z * VertCountX + x + 0; tris[triOffset + 1] = z * VertCountX + x + VertCountX + 0; tris[triOffset + 2] = z * VertCountX + x + VertCountX + 1; //Second Triangle tris[triOffset + 3] = z * VertCountX + x + 0; tris[triOffset + 4] = z * VertCountX + x + VertCountX + 1; tris[triOffset + 5] = z * VertCountX + x + 1; } } // Generate Mesh and populate with Data Mesh mesh = new Mesh(); mesh.vertices = verts; mesh.triangles = tris; mesh.normals = norm; mesh.uv = uv; MeshFilter meshfilter = GetComponent(); MeshRenderer meshRenderer = GetComponent(); MeshCollider meshCollider = GetComponent(); meshfilter.mesh = mesh; meshfilter.renderer.material = material; meshCollider.sharedMesh = meshfilter.mesh; } }

Viewing all articles
Browse latest Browse all 171066

Trending Articles