Search code examples
c#unity-game-engineterrain

Hide/show terrain programmatically


How to hide/show terrain and other gameObjects during run-time (by C# code)?
I want to show/hide terrain and gameObjects in a fade-in/out style


Solution

  • The Terrain script uses a built-in shader by default. This one is not accessible nor modifiable.

    Material mat = terrain.materialTemplate;
    Debug.Log(mat==null); // True
    

    Assigning a custom shader is limited to the Nature shader and none offers the use of Color. It seems in general that the terrain ignores the color of a shader.

        Material mat = terrain.materialTemplate;
        Color c = mat.color;
        c.a = 0.5f;  
        mat.color = c;
    

    This will show the modification in the shader color but no effect on the terrain itself.

    As a result you would have to create your own shader, like this one http://wiki.unity3d.com/index.php/TerrainTransparency