-1

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Al-Hanash Moataz
  • 385
  • 4
  • 14

2 Answers2

1

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

Everts
  • 10,408
  • 2
  • 34
  • 45
0
public class ChangeAlpha : MonoBehaviour {
public Renderer Renderer;
private Material _material;

// Use this for initialization
void Start () {
    _material = Renderer.material;
    StartCoroutine("ChangeAlphaSlowly");
}

private IEnumerator ChangeAlphaSlowly()
{
    var increaseAmount = 0f;
    var _color = new Color(0, 0, 1, 0);
    for (int i = 0; i < 10; i++)//ten step 
    {
        increaseAmount += 0.1f;
        _material.color = new Color(0, 0, 1, increaseAmount);          
        yield return new WaitForSeconds(0.1f);
    }

}

}

Halil Cosgun
  • 427
  • 3
  • 10
  • 24