change terrain texture and tree at runtime

I checked the documentation but I have not found anything that I serve.
how do I change at runtime the texture of the grass and the trees of the terrain?

edit 19.07.2012

use this script. to see how it works do the following:

  1. make a terrain and paint it with 3 any textures
  2. add this script to any GameObject
  3. assign terrain to script (Terrain field)
  4. run the game
  5. press ‘space’ button and check that on button pressing textures 1 and 2 are changing
  6. read script’s comments for more details
using UnityEngine;
using System.Collections;
public class TerrainTextureChanger : MonoBehaviour
    public Terrain terrain;
    void Update()
        if (Input.GetKeyDown(KeyCode.Space))
            //switch all painted in texture 1 to texture 2
            UpdateTerrainTexture(terrain.terrainData, 1, 2);
        if (Input.GetKeyUp(KeyCode.Space))
            //switch all painted in texture 2 to texture 1
            UpdateTerrainTexture(terrain.terrainData, 2, 1);
    static void UpdateTerrainTexture(TerrainData terrainData, int textureNumberFrom, int textureNumberTo)
        //get current paint mask
        float[, ,] alphas = terrainData.GetAlphamaps(0, 0, terrainData.alphamapWidth, terrainData.alphamapHeight);
        // make sure every grid on the terrain is modified
        for (int i = 0; i < terrainData.alphamapWidth; i++)
            for (int j = 0; j < terrainData.alphamapHeight; j++)
                //for each point of mask do:
                //paint all from old texture to new texture (saving already painted in new texture)
                alphas[i, j, textureNumberTo] = Mathf.Max(alphas[i, j, textureNumberFrom], alphas[i, j, textureNumberTo]);
                //set old texture mask to zero
                alphas[i, j, textureNumberFrom] = 0f;
        // apply the new alpha
        terrainData.SetAlphamaps(0, 0, alphas);

wrong answer below!

look for


should work. write here if you stuck somewhere on this way 8)