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. Dismiss Notice

Problems with nested loop

Discussion in 'Scripting' started by Vexer, Jul 13, 2018.

  1. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Hey guys im trying to create a 5x5 grid with my cubes but for some reason my script only spawns one cube what am i doing wrong?
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4. using UnityEditor;
    5.  
    6. [RequireComponent(typeof(MeshFilter))]
    7. [RequireComponent(typeof(MeshRenderer))]
    8. public class MapGenerator : MonoBehaviour
    9. {
    10.     float offSet = 0.5f;
    11.     public int gridSize = 10;
    12.  
    13.     void Start()
    14.     {
    15.         for (int i = 0; i < gridSize; i++)
    16.         {
    17.             for (int j = 0; j < gridSize; j++)
    18.             {
    19.                 CreateCube(i * offSet, 0, j * offSet);
    20.             }
    21.         }
    22.     }
    23.  
    24.     void CreateCube(float xPos, float yPos, float zPos)
    25.     {
    26.         Vector3[] vertices = {
    27.             new Vector3 (0, 0, 0),
    28.             new Vector3 (0.5f, 0, 0),
    29.             new Vector3 (0.5f, 0.5f, 0),
    30.             new Vector3 (0, 0.5f, 0),
    31.             new Vector3 (0, 0.5f, 0.5f),
    32.             new Vector3 (0.5f, 0.5f, 0.5f),
    33.             new Vector3 (0.5f, 0, 0.5f),
    34.             new Vector3 (0, 0, 0.5f),
    35.         };
    36.  
    37.         int[] triangles = {
    38.          0, 2, 1, //face front
    39.          0, 3, 2,
    40.          2, 3, 4, //face top
    41.          2, 4, 5,
    42.          1, 2, 5, //face right
    43.          1, 5, 6,
    44.          0, 7, 4, //face left
    45.          0, 4, 3,
    46.          5, 4, 7, //face back
    47.          5, 7, 6,
    48.          0, 6, 7, //face bottom
    49.          0, 1, 6
    50.          };
    51.  
    52.         Mesh mesh = GetComponent<MeshFilter>().mesh;
    53.         mesh.Clear();
    54.         mesh.vertices = vertices;
    55.         mesh.triangles = triangles;
    56.         this.GetComponent<Renderer>().material.color = Color.green;
    57.         MeshUtility.Optimize(mesh);
    58.         mesh.RecalculateNormals();
    59.     }
    60. }
    61.  
     
  2. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    208
    Are you sure that you are only creating one single cube, and not correct amount of cubes but they all end up at the same position?
    Try adding
    Code (CSharp):
    1. transform.position = new Vector3( xPos, yPos, zPos );
    at the end of your CreateCube() member.

    Also change the gridSize to 5 instead of 10 if you want a 5x5 grid, currently you're iterating through a 10x10 sized grid :)

    //Edit
    Took Another look and noticed that your never creating new mesh filters/renderers or objects, but Always re-create the mesh for the Components attached to the same object as the script is. This is likely what makes your script only create one single cube.
     
  3. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    so how would i fix that?
     
  4. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    I added this to it:
    Code (csharp):
    1.  
    2. gameObject.AddComponent<MeshRenderer>();
    3.         gameObject.AddComponent<MeshCollider>();
    4.  
    But that only created extra's on the game object itself instead of creating childs with mesh filters etc as it should i think?
     
  5. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    208
    The line in posted will fix the issue of your objects beeing positioned at the same Place, if you wonder about what I added to my post though I modified your code for this

    Code (CSharp):
    1.  void CreateCube(float xPos, float yPos, float zPos)
    2.     {
    3.         GameObject go = new GameObject( "Cube" );  //Create a new game object for every cube
    4.  
    5.         Vector3[] vertices = {
    6.             new Vector3 (0, 0, 0),
    7.             new Vector3 (0.5f, 0, 0),
    8.             new Vector3 (0.5f, 0.5f, 0),
    9.             new Vector3 (0, 0.5f, 0),
    10.             new Vector3 (0, 0.5f, 0.5f),
    11.             new Vector3 (0.5f, 0.5f, 0.5f),
    12.             new Vector3 (0.5f, 0, 0.5f),
    13.             new Vector3 (0, 0, 0.5f),
    14.         };
    15.         int[] triangles = {
    16.          0, 2, 1, //face front
    17.          0, 3, 2,
    18.          2, 3, 4, //face top
    19.          2, 4, 5,
    20.          1, 2, 5, //face right
    21.          1, 5, 6,
    22.          0, 7, 4, //face left
    23.          0, 4, 3,
    24.          5, 4, 7, //face back
    25.          5, 7, 6,
    26.          0, 6, 7, //face bottom
    27.          0, 1, 6
    28.          };
    29.         MeshFilter mf = go.AddComponent< MeshFilter >();  //Attach a mesh filter Component to the new game object
    30.         Mesh mesh = mf.mesh;
    31.         mesh.vertices = vertices;
    32.         mesh.triangles = triangles;
    33.         MeshRenderer mr = go.AddComponent< MeshRenderer >();     //Attach a new mesh renderer to the cube
    34.          mr.material.color = Color.green;
    35.         MeshUtility.Optimize(mesh);
    36.         mesh.RecalculateNormals();
    37.  
    38.          go.transform.position = new Vector3( xPos, yPos, zPos );  //Reposition the new game object to it's assigned position
    39.     }
     
  6. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Thank you so much this fixed it! but do you have any idea how i can make my cubes color green without these kind of shadows or something?? https://gyazo.com/ce9ec54515743444e6dd5fcc205be604
     
  7. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    208
    Haven't tried myself but I assume that your cubes are assigned unity's default material with it's default shader which is probably what cause the shadows. You would probably want to create your own shader or find a shader that computes shading differently, something like Lightning beeing determined by World position or something instead of normal direction. I'm not even sure that's right to be honest, can't give you much input on shaders since I am quite unfamiliar with them.

    When you have your new material that's using the shader of your desire simply assign it to the mesh renderer in your cube-generation code like so:

    Code (CSharp):
    1. public Material myMaterial;    //The material to be assigned to every cube
    2.  
    3. //Assign material to the cube that's currently beeing generated during your CreateCube() call
    4. MeshRenderer mr = go.AddComponent< MeshRenderer >();
    5. mr.material = myMaterial;
    //Edit
    An alternative to this would be to create only one mesh renderer that receive all verts/tris of your cubes. This would require you to include every existing verts back to the Array of verts/tris in your mesh filter though prior to adding the most recently created verts and tris.

    I Think there's actually a tutorial explaining this quite thoroughly in Unity3d.com's Learn page, explaining procedurally generated Environments and how to create them- which include this issue!
     
    Last edited: Jul 14, 2018