- Home /
How can I clone a Terrain object? (Not the reference)
I'm trying to create a procedural terrain generator based on Simplex Noise. I have an array of terrain objects and the problem is that when I fill this array with the function Instantiate() but it generates only a reference to the original terrain object so when I edit the heights of a single terrain i get all the terrains with the same shape. So, how do I copy a Terrain object without reference?
This is the code which generates the tile from the original tile:
int x = (j - buffer / 2) + currentTile.x;
int z = (i - buffer / 2) + currentTile.z;
TerrainTile tile = new TerrainTile (x, z);
tile.tileTerrain = Instantiate (terrainBase, new Vector3 (x * tileSize, 0, z * tileSize), Quaternion.identity) as Terrain;
tile.tileTerrain.transform.gameObject.SetActive (true);
return tile;
TerrainTile is a class i made:
class TerrainTile
{
public Terrain tileTerrain;
public int x;
public int z;
public TerrainTile (int x, int z)
{
this.x = x;
this.z = z;
}
}
Answer by MakeCodeNow · Jan 03, 2015 at 06:15 AM
You need to make a new TerrainData, too, and point the new terrain at that.
Ok thanks I solved my problem. I used Object.Instantiate(terrainData) to duplicate the terrainData for every single Terrain tile
$$anonymous$$ake sure you then give the instantiated object new terrain data otherwise your new object will share terraindata with the previous object. So if you apply a height map to object 2, object 1 will have the same height map.
I asked a question about this earlier this week when I was at the step your at now.