How to create TerrainData at runtime?

I’m using the following code:

Terrain terrain = GetComponent();
TerrainData terrainData = new TerrainData();

    terrainData.heightmapResolution = 513;
    terrainData.size = new Vector3(2000, 600, 2000);

    terrainData.heightmapResolution = 512;
    terrainData.baseMapResolution = 1024;
    terrainData.SetDetailResolution(32, 8);

    AssetDatabase.CreateAsset(terrainData, string.Format("Assets/New Terrain {0}.asset", n++));
    terrain.terrainData = terrainData;
    Debug.Log(terrainData.heightmapScale);
    terrain.Flush();

but the terrain it creates doesn’t show up. There are no error messages but nothing appears when I run this code. Is there something I’m missing?

Hi, this is the code what you want:

TerrainData tData = new TerrainData();
tData.size = new Vector3(512,512,512);
Terrain.CreateTerrainGameObject(tData);