title: Size issues when creating a procedural mesh generator in Unity
body: <p>I've been working on building a procedural generator for Unity, that takes in noise and uses it to build a height map.</p>
  
  <p>So far, everything seems to work as long as I limit the size of the mesh to around 250x250. If I attempt to make a larger mesh, the script won't calculate it.</p>
  
  <p>The puzzling thing is that I get no memory errors or anything of that nature. I've implemented a Regenerate button that allows me to generate a new mesh in Unity and as long as I remain in the range of 250x250 or less, it works fine. If I pick a larger value, the mesh simply remains unchanged.</p>
  
  <p>How I calculate the Mesh:</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  using UnityEditor;
  using System.IO;
  
  [ExecuteInEditMode]
  [RequireComponent(typeof(MeshFilter))]
  [RequireComponent(typeof(Noise))]
  public class CustomTerrain : MonoBehaviour {
      //Note: sizeX*sizeZ determines mesh size.
      public int sizeX = 4; //These values are public so they can be
      public int sizeZ = 4; //modified in the Unity Editor.
      public float noiseSize = 1.0f; 
      public float cellSize = 1.0f;
      private string mPath = "Assets/Generated/";
      Noise noise;
  
      void Start() {
          noise = GetComponent&lt;Noise&gt;();
          this.LoadMesh();
          if (!GetComponent&lt;MeshFilter&gt;().sharedMesh) {
              this.Regenerate();
          }
      }
      public void LoadMesh() {
          Mesh mesh = Instantiate(AssetDatabase.LoadMainAssetAtPath(mPath + gameObject.name + ".asset") as Mesh);
          if (mesh) {
              GetComponent&lt;MeshFilter&gt;().mesh = mesh;
          }
          recalculateMeshCollider(mesh);
      }
  
      Vector3[] GenVertices() {
          float x, z;
          int w = (sizeX+1);
          int l = (sizeZ+1);
          Vector3[] vertices = new Vector3[w*l];
          for (int gx = 0; gx &lt; w; gx++) {
              for (int gz = 0; gz &lt; l; gz++) {
                  x = gx*cellSize;
                  z = gz*cellSize;
                  float height = (noiseSize * noise.Get(x,z));
                  vertices[gx*l+gz] = new Vector3(x, height, z);
              }
          }
          return vertices;
      }
  
      int[] GenTriangles() {
          int vertciesPerTriangle = 3;
          int trianglesPerCell = 2;
          int numberCells = sizeX * sizeZ;
          int[] triangles = new int[vertciesPerTriangle * trianglesPerCell * numberCells];
  
          int tIndeX = 0;
          for (int cX = 0; cX &lt; sizeX; cX++) {
              for (int cZ = 0; cZ &lt; sizeZ; cZ++) {
                  int n = cX*(sizeZ+1)+cZ;
  
                  triangles[tIndeX] = n;
                  triangles[tIndeX+1] = n+1;
                  triangles[tIndeX+2] = n+sizeZ+2;
                  triangles[tIndeX+3] = n;
                  triangles[tIndeX+4] = n+sizeZ+2;
                  triangles[tIndeX+5] = n+sizeZ+1;
                  tIndeX +=6;
              }
          }
          return triangles;
      }
  
      Vector2[] GenUVs() {
          int w = (sizeX + 1);
          int l = (sizeZ + 1);
          Vector2[] uvs = new Vector2[w * l];
  
          for (int uX = 0; uX &lt; w; uX++) {
              for (int uZ = 0; uZ &lt; l; uZ++) {
                  uvs[uX*l+uZ] = new Vector2((float)uX/sizeX, (float)uZ/sizeZ);
              }
          }
          return uvs;
      }
  }
  </code></pre>
  
  <p>My Regenerate function:</p>
  
  <pre><code>public void Regenerate() {
      noise.Init();
      Mesh mesh = GetComponent&lt;MeshFilter&gt;().sharedMesh;
      if(!mesh) {
          mesh = new Mesh();
          GetComponent&lt;MeshFilter&gt;().sharedMesh = mesh;
      }
      mesh.vertices = GenVertices();
      mesh.triangles = GenTriangles();
      mesh.uv = GenUVs();
      mesh.RecalculateNormals();
      recalculateMeshCollider(mesh);
  }
  
  public void recalculateMeshCollider(Mesh mesh) {
      if (GetComponent&lt;MeshCollider&gt;()) {
          DestroyImmediate(GetComponent&lt;MeshCollider&gt;());
      }
      transform.gameObject.AddComponent&lt;MeshCollider&gt;();
      transform.GetComponent&lt;MeshCollider&gt;().sharedMesh = mesh;
  }
  </code></pre>