For loop and instantiate issue

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)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelGen : MonoBehaviour
{
    static int mapwidth = 10;
    static int maphight = 10;
    static int tilesize = 64;
    public GameObject Grass;
    public GameObject Stone;
    static Vector3 mapsize = new Vector3(0,0,0);
    static int tileid = 0;



    void Start()
    {
        for (int i = 0; i < mapwidth; i++)
        {
            for (int a = 0; a < maphight; a++)
            {
                tileid = Random.Range(1, 10);
                if (tileid <= 10)
                {
                    GameObject GrassClone = Instantiate(Grass, mapsize, Quaternion.identity);
                    mapsize.x = mapsize.x + tilesize;
                    Debug.Log("Grass Created at" + mapsize);
                }
                else if (tileid == 1)
                {
                    GameObject StoneClone = Instantiate(Stone, mapsize, Quaternion.identity);
                    mapsize.x = mapsize + tilesize;
                    Debug.Log("Stone Created at " + mapsize);
                }
                mapsize.y = mapsize.y + tilesize;
            }

        }
          
    }



}

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?

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.

1 Like

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelGen : MonoBehaviour
{
    static int mapwidth = 10;
    static int maphight = 10;
    static int tilesize = 64;
    public GameObject Grass;
    public GameObject Stone;
    static Vector3 mapsize = new Vector3(0,0,0);
    static int tileid = 0;



    void Start()
    {
        for (int i = 0; i < mapwidth; i++)
        {

            for (int a = 0; a < maphight; a++)
            {
                tileid = Random.Range(1, 10);
                if (tileid >=2)
                {
                    GameObject GrassClone = Instantiate(Grass, mapsize, Quaternion.identity);
               
                    Debug.Log("Grass Created at" + mapsize);
                }
                else if (tileid == 1)
                {
                    GameObject StoneClone = Instantiate(Stone, mapsize, Quaternion.identity);
                    mapsize.x = mapsize.x + tilesize;
                    Debug.Log("Stone Created at " + mapsize);
                }

                mapsize.y = mapsize.y + tilesize;
            }
            mapsize.x = mapsize.x + tilesize;
            mapsize.y = 64;
        }
          
    }



}

but the Instantiate still are not visible in game do i need to change a value in the object to make it visable?

Update Issue was the position of the object needed to be 0.64 not 64 however 0.64 wont be accepted as a vector