Can't set dynamically GameObject position

I have a stranger problem. I want generate a flat tiled terrain for a personal test and the way I follow is:

  • Create custom primitive
  • Set a tiled group with the same material (child of empty gameobject)
  • Put group in an empty gameobject for generate a large tiled map (child of main gameobject)

Everything works, but the last section, when I generate map, all empty gameobject spawn at the same space but with correct value in the inspector.

TerrainGeneration.cs
```csharp
**// Main Camera
public GameObject mainCamera;

// Area Block GameObject
private GameObject areaBlock;
private Transform t;

// Terrain Cube
private GameObject tiledCube;

// Tiled Block size
public int blockSize;

// Cube Renderer
public Material mats;

// Terrain Size
public int xSize = 1;
public int zSize = 1;

private void Start()
{
// Generate Terrain
GenerateTiledTerrain();

// Center Camera to terrain
mainCamera.transform.position = new Vector3((xSize * blockSize) / 2, 5, (zSize * blockSize) / 2);

}

private void GenerateTiledCube()
{
// Create a terrain block
tiledCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
tiledCube.transform.localPosition = new Vector3(0, 0, 0);
tiledCube.transform.localScale = new Vector3(1, 0.1F, 1);
//tiledCube.isStatic = true;

// Assing material to terrain block
int randomMaterial;

randomMaterial = Random.Range(0, mats.Length);
tiledCube.GetComponent<MeshRenderer>().sharedMaterial = mats[randomMaterial];

Destroy(tiledCube);

int count = 0;

// Generate tiled block
for (int i = 0; i < blockSize; i++)
{
    for (int j = 0; j < blockSize; j++)
    {
        count++;

        tiledCube.name = "Cube_" + count;
        Instantiate(tiledCube, new Vector3(i, 0, j), Quaternion.identity, areaBlock.transform);
    }
}

}

private void GenerateTiledTerrain()
{
Destroy(areaBlock);

int count = 0;

for (int i = 0; i < xSize; i++)
{
    for(int j = 0; j < zSize; j++)
    {
        count++;
        areaBlock = new GameObject("AreaBlock_" + count);
        areaBlock.transform.parent = gameObject.transform;
        areaBlock.transform.localPosition = new Vector3(i * blockSize, 0, j * blockSize);
        GenerateTiledCube();
        //Instantiate(areaBlock, new Vector3(i * blockSize, 0, j * blockSize), Quaternion.identity, gameObject.transform);

    }
}

}**
```

In the first image the z position in the inspector is correct for all Areas, but the location it’s the same. If I reset all transforms the Areas expand in the correct location but all coordinates are 0.

I have to use new GameObject instead Instatiate() because if I try to instantiate, the position is correct but every clone get the previous child: the first gameobject have 16 children, the second 32, 64…

How can I fix this problem?

Thanks in advance.

Uh, why is there a Destroy(tiledCube); in the middle of your generation function? Also, I think you should use a prefab for the tile, that would be assigned from the inspector, and instantiate this prefab, this is supposed to solve the multiple child problem.

Thanks for reply, I call Destroy() method for remove the first primitive created. I try to use a prefab for areaBlock and the result it’s the same (by Instatiate prefab). Tomorrow i try to create a tileBlock prefab.

If i create an AreaBlock prefab with child, obviously work, but isn’t perfect becouse the player has the possibility to change the blockSize and the tiledMap size. I try to create a prefab for cube and for areaBlock but with the Instantiate() method it’s the same result and the vertex count go to 1.6M instead 10K becouse every instance of AreaBlock clone the number of child of previous instance.

There is a solution?

Fixed, the problem was that I was thinking about what the game should do from start to finish, so first it creates tiledBlock and then assigns it to each area. The solution is reverse thinking: first generates the areas, then a tiledBlock loop is assigned to each area. In this way I solved the problem of the clones that took the children of the previous. I hope at least it can be of help to someone, even from a conceptual point of view.

Good programming.