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.