Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

My procedural grid doesnt genearate on Z axis, please help!

Discussion in 'Scripting' started by Bicaro, Sep 21, 2018.

  1. Bicaro

    Bicaro

    Joined:
    Aug 12, 2017
    Posts:
    8
    Let's say grid is 10x10, so the grid generates 60 vertices(in one row), 3 for each triangle (so it doesnt make a gradient look), and 2 triangles for each quad. And it generates 10 of them in x axis, but if i put z axis any number more than 1, none of the triangles generate, not even on the x axis. Triangles array doesnt run out of space.
    I dont understand the problem, please help! :(

    Here are pictures to help:
    http://prntscr.com/kx3u6f
    http://prntscr.com/kx3ul0

    This is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
    6. public class CustomMesh : MonoBehaviour {
    7.  
    8.     Mesh mesh;
    9.     Vector3[] vertices;
    10.     int[] triangles;
    11.  
    12.     public int xSize;
    13.     public int ySize;
    14.     public bool gizmos = false;
    15.  
    16.     void Awake()
    17.     {
    18.         Generate();
    19.     }
    20.  
    21.     void Generate()
    22.     {
    23.         GetComponent<MeshFilter>().mesh = mesh = new Mesh();
    24.         mesh.name = "Procedural Grid";
    25.  
    26.         vertices = new Vector3[xSize * ySize * 6];
    27.         triangles = new int[xSize * ySize * 6];
    28.  
    29.         for (int y = 0, i = 0; y < ySize;)
    30.         {
    31.             for (int x = 0; x < xSize; x++)
    32.             {
    33.  
    34.                 vertices[i] = new Vector3(x, 0, y);
    35.                 vertices[i + 1] = new Vector3(x, 0, y + 1);
    36.                 vertices[i + 2] = new Vector3(x + 1, 0, y);
    37.                 vertices[i + 3] = new Vector3(x, 0, y + 1);
    38.                 vertices[i + 4] = new Vector3(x + 1, 0, y + 1);
    39.                 vertices[i + 5] = new Vector3(x + 1, 0, y);
    40.  
    41.                 i += 6;
    42.             }
    43.             y++;
    44.         }
    45.  
    46.         mesh.vertices = vertices;
    47.  
    48.         for(int y = 0, ti = 0; y < ySize;)
    49.         {
    50.             for(int x = 0; x < xSize;)
    51.             {
    52.                 triangles[ti] = ti;
    53.                 triangles[ti + 1] = (ti + 1);
    54.                 triangles[ti + 2] = (ti + 2);
    55.                 triangles[ti + 3] = (ti + 3);
    56.                 triangles[ti + 4] = (ti + 4);
    57.                 triangles[ti + 5] = (ti + 5);
    58.  
    59.                 ti += 6;
    60.                 x++;
    61.             }
    62.             ti += 6;
    63.             y++;
    64.         }
    65.      
    66.         mesh.triangles = triangles;
    67.         mesh.RecalculateNormals();
    68.     }
    69.  
    70.     private void OnDrawGizmos()
    71.     {
    72.         if (gizmos == true)
    73.         {
    74.             Gizmos.color = Color.black;
    75.             for (int i = 0; i < vertices.Length; i++)
    76.             {
    77.                 Gizmos.DrawSphere(vertices[i], 0.1f);
    78.             }
    79.  
    80.             if (vertices == null)
    81.             {
    82.                 return;
    83.             }
    84.         }
    85.     }
    86. }
     
  2. wtrebella

    wtrebella

    Joined:
    Mar 18, 2013
    Posts:
    35
    The problem is the second "ti += 6", in line 62. Remove that and it works!
     
    Diablo404 likes this.
  3. Bicaro

    Bicaro

    Joined:
    Aug 12, 2017
    Posts:
    8
    Seriously dude you're a life saver, thank you ;)
     
  4. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Sharp as a tack!