Game object being instantiated twice

Hi, I am creating empty game object in a grid, however the code below causes two game objects to spawn76717-capture.png

The “Non cloned” instance of each game object doesn’t even have the correct x/z positions they are all set at 0, 0, 0

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

public class TerainGen : MonoBehaviour
{

public Plane terrainChunk;
public float width = 1f;
public float height = 1f;

public int MapSize = 10;
public int x, z;

public Material grass;

public GameObject Astar;
Grid grid;

void Start()
{
    createChunks();

}

public void createChunks() {
    for (int x = 0; x < MapSize; x++)
    {
        for (int z = 0; z < MapSize; z++)
        {
            Debug.Log("Chunk x" + x + " z " + z);
          Instantiate(new GameObject("Chunk x" + x + " z " + z), new Vector3(x * 25, 0, z * 25), Quaternion.identity);

        }
    }
}
}

With

Instantiate(new GameObject("Chunk x" + x + " z " + z), new Vector3(x * 25, 0, z * 25), Quaternion.identity);

you are creating a new gameobject (the “non-clone”) and immediately instantiate (create) the same object as clone … The gameobject created with the “new” keyword is the original and the instantiated the “clone” from the original.

Create a prefab and instantiate this.

public GameObject m_yourPrefab;
...

public void createChunks() 
{
...
    GameObject go = Instantiate(m_yourPrefab, new Vector3(x * 25, 0, z * 25), Quaternion.identity) as GameObject;
    go.name = "Chunk x" + x + " z " + z;
...
}