How to generate multiple Terrain objects with different TerrainData and Splat Texture via C# script?

I’m trying to stream image and elevation data from a remote server for rendering maps in Unity. Since the server is a Tile Map Service (TMS), the images and elevation data are stored in tiles. I created a terrain prefab called mymap_terrain, and instantiate it in my GameController (which is an empty GameObject). Just for a test case, when I hit run, the GameController instantiates two instances of terrain with different textures and elevation data (ideally). Here is my code used for instantiation of terrain objects. The issue is described after:

function Start () {
	terrain = Instantiate(terrainPrefab, Vector3(0.0, 0.0, 0.0), Quaternion.identity);
	terrain.gameObject.name = "mapL6X124Y16";
	
	yield WaitForSeconds(5.0);
	terrain2 = Instantiate(terrainPrefab, Vector3(15.0, 0.0, 15.0), Quaternion.identity);
	terrain2.gameObject.name = "mapL6X125Y17";
}

Here is my method that is attached to the terrain prefab, that I use to set various parameters of the terrain:

private IEnumerator terrainDataSet(int layer, int Xtile, int Ytile, float[,] heightData) {
		float tiles = Mathf.Pow (2, 2*layer+1);
		float length = (float)(2048.0/(Mathf.Sqrt(2*tiles)));
            //Get the terrain component of this terrain object
		Terrain terrain = (Terrain)GetComponent("Terrain");
		terrain.terrainData.heightmapResolution = 33;
		terrain.terrainData.SetHeights(0,0,heightData);
		terrain.terrainData.size = new Vector3(length,5,length);
		
		
		WWW www = new WWW(tms_URL + layer + "/" + Xtile + "/" + Ytile + ".jpg");
		while(!www.isDone){
			yield return null;	
		}
		Texture2D myTexture = new Texture2D(256, 256,TextureFormat.RGB24,false);
		www.LoadImageIntoTexture(myTexture);
		//Set Texture for the terrain
		SplatPrototype splats = new SplatPrototype(); 
		splats.tileSize = new Vector2(length, length);
		splats.texture = myTexture;

		terrain.GetComponent<Terrain>().terrainData.splatPrototypes = new SplatPrototype[] {splats};
		yield break;
	}

When I load terrain1 with 1.tif elevation data and 1.jpg texture, it’s perfect! I wait for 5 seconds and load terrain2 with 2.tif elevation data and 2.jpg texture. When I do this, it automatically changes terrain1 to have the texture and elevation data of terrain2. Also, When I go to either terrain’s inspector and change the terrain length/width/etc, it is reflected on both terrains. Any way that I can have two independent terrains with their own set of texture and elevation data? Some images down here to help you understand better:

terrain1

both terrains loaded. Changes texture and elevation data for terrain 1

Any help/comment is appreciated :slight_smile: … I’m new to Unity (about a month of using the software)

I think the problem is that you take the Terrain data from terrain, try to create a new Terrain Data and assign it to terrain.

This how i would generate several terrains

for (int i = 0; i < numberOfTerrains; i++)
{
    for (int j = 0; j < numberOfTerrains; j++)
    {
        TerrainData _terrainData = new TerrainData();
        terrain.terrainData.heightmapResolution = 33;
        terrain.terrainData.SetHeights(0,0,heightData);
        terrain.terrainData.size = new Vector3(length,5,length);
        GameObject _terrain = Terrain.CreateTerrainGameObject(_terrainData);
        _terrain.transform.position = new Vector3(_terrain.GetComponent<Terrain>().terrainData.size.x * i, 0, _terrain.GetComponent<Terrain>().terrainData.size.x * j);

    }
}