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

For loop and instantiate issue

Discussion in 'Scripting' started by Protexxi, Dec 1, 2019.

  1. Protexxi

    Protexxi

    Joined:
    Oct 4, 2019
    Posts:
    7
    Hello,

    i'm attempting to create a top down terrain using 64 x 64 sprites;
    i currently have 2 sprites stone and grass.

    i have the following script that uses to for loops to copy the sprites using the instantiate function into a 10 x 10 grid (i understand is this a messy and inefficient way of doing this but ill change it out at a later date)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LevelGen : MonoBehaviour
    6. {
    7.     static int mapwidth = 10;
    8.     static int maphight = 10;
    9.     static int tilesize = 64;
    10.     public GameObject Grass;
    11.     public GameObject Stone;
    12.     static Vector3 mapsize = new Vector3(0,0,0);
    13.     static int tileid = 0;
    14.  
    15.  
    16.  
    17.     void Start()
    18.     {
    19.         for (int i = 0; i < mapwidth; i++)
    20.         {
    21.             for (int a = 0; a < maphight; a++)
    22.             {
    23.                 tileid = Random.Range(1, 10);
    24.                 if (tileid <= 10)
    25.                 {
    26.                     GameObject GrassClone = Instantiate(Grass, mapsize, Quaternion.identity);
    27.                     mapsize.x = mapsize.x + tilesize;
    28.                     Debug.Log("Grass Created at" + mapsize);
    29.                 }
    30.                 else if (tileid == 1)
    31.                 {
    32.                     GameObject StoneClone = Instantiate(Stone, mapsize, Quaternion.identity);
    33.                     mapsize.x = mapsize + tilesize;
    34.                     Debug.Log("Stone Created at " + mapsize);
    35.                 }
    36.                 mapsize.y = mapsize.y + tilesize;
    37.             }
    38.  
    39.         }
    40.          
    41.     }
    42.  
    43.  
    44.  
    45. }
    46.  
    47.  
    The expected outcome of the script is as stated above a 10 x 10 grid of sprites.

    the actual outcome of the script is a infinite number of clones of the sprite non of which are actually viable in the game.

    what am i missing and or doing wrong?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Do you have this script on the things you are instantiating? Because each one of them would then run its 10x10 thing again... and again... and again...

    One neat trick us you can turn Start() into a Coroutine by declaring it as
    IEnumerator Start()
    and then put a
    yield return null;
    in your loop and watch in real time what is happening, pause the editor, study GameObjects, etc.

    That way you can see the Debug.Log() outputs, pause, root around in the scene and try to track down what's going on. You can also search the Hierarchy window for specific instances of scripts by putting
    t:MyScriptName
    in the search field, which is VERY handy.
     
    Protexxi likes this.
  3. Protexxi

    Protexxi

    Joined:
    Oct 4, 2019
    Posts:
    7
    Thank you! created a 2nd Copy of the object without the script to use for instantiating worked great and makes 100 game objects however the objects are still not viable fixed the maths to correctly line the grid. script now looks like this
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LevelGen : MonoBehaviour
    6. {
    7.     static int mapwidth = 10;
    8.     static int maphight = 10;
    9.     static int tilesize = 64;
    10.     public GameObject Grass;
    11.     public GameObject Stone;
    12.     static Vector3 mapsize = new Vector3(0,0,0);
    13.     static int tileid = 0;
    14.  
    15.  
    16.  
    17.     void Start()
    18.     {
    19.         for (int i = 0; i < mapwidth; i++)
    20.         {
    21.  
    22.             for (int a = 0; a < maphight; a++)
    23.             {
    24.                 tileid = Random.Range(1, 10);
    25.                 if (tileid >=2)
    26.                 {
    27.                     GameObject GrassClone = Instantiate(Grass, mapsize, Quaternion.identity);
    28.                
    29.                     Debug.Log("Grass Created at" + mapsize);
    30.                 }
    31.                 else if (tileid == 1)
    32.                 {
    33.                     GameObject StoneClone = Instantiate(Stone, mapsize, Quaternion.identity);
    34.                     mapsize.x = mapsize.x + tilesize;
    35.                     Debug.Log("Stone Created at " + mapsize);
    36.                 }
    37.  
    38.                 mapsize.y = mapsize.y + tilesize;
    39.             }
    40.             mapsize.x = mapsize.x + tilesize;
    41.             mapsize.y = 64;
    42.         }
    43.          
    44.     }
    45.  
    46.  
    47.  
    48. }
    49.  
    50.  
    but the Instantiate still are not visible in game do i need to change a value in the object to make it visable?
     
  4. Protexxi

    Protexxi

    Joined:
    Oct 4, 2019
    Posts:
    7
    Update Issue was the position of the object needed to be 0.64 not 64 however 0.64 wont be accepted as a vector