Rendering a terrain game object dynamically

Hi!

So I’m having a little problem with using scripting to create a new terrain game object and render it. I’ve got the terrain “FlatTerrain” in my Assets/Resources folder which I am loading, getting it’s terrainData object to create a new terrain from, then using the TerrainToolKit to generate some terrain features and finally setting the terrain renderer to display the terrain.

Nothing is happening when I run this code and I get no warnings or errors.

Any help much appreciated!

    void Start () 
    {
        //get the base flat terrain
        Terrain terrain = (Terrain) Resources.Load("FlatTerrain");
        //create a new terrain using the flat terrains default data
        GameObject newTerrain = Terrain.CreateTerrainGameObject(terrain.terrainData);
        //add the terrain generation component to the new terrain
        newTerrain.AddComponent<TerrainToolkit>();
        //now do the generation 
        ((TerrainToolkit)newTerrain.GetComponent("TerrainToolkit")).PerlinGenerator(4, 1.0f, 8, 0.1f);
        //display the new terrain
        newTerrain.renderer.enabled = true;
    }

Found out what it was!

“FlatTerrain” is of type TerrainData not Terrain! Changing that fixed it. The reason I was getting no errors is because I had forgotten to add that script to my main camera… yep, that’s embarrasing! :wink:

These things happen to everyone :slight_smile: I spent half an hour trying to debug some shop code, only to realise I had used a < instead of a >. D’oh!

And don’t get me started on pulling apart every line code I had because I had the collapse button toggled on in the console view :slight_smile: