- Home /
How to give a Terrain a material at runtime?
I'm working on a terrain grid generated at runtime. The terrains are all created programmatically, but I can't figure out how to give it a material as you would through the inspector in this field here highlighted in blue:
I tried adding a Renderer to the Terrain component, but it gave me an error saying that the field was read only. I'm currently generating the terrains at runtime using perlin noise.
Edit: I should add that I'm using Terrain.CreateTerrainGameObject() to create the terrains.
Answer by AlucardJay · May 23, 2014 at 11:19 PM
To access and modify a terrain material :
Terrain.materialTemplate
Tested with this script :
public var terrainObject : Terrain;
public var terrainMaterial : Material;
function Update()
{
if ( Input.GetMouseButtonDown(0) )
{
terrainObject.materialTemplate = terrainMaterial;
}
}
Thanks, that worked! It's a pain that field wasn't documented.
Your answer
![](https://koobas.hobune.stream/wayback/20220613145738im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Larger or smaller terrain chunks? 2 Answers
Remove actions in Scene 0 Answers
Should a make a large terrain or should I use several terrains? 1 Answer
TerrainData doesn't save lightmap. 0 Answers
How can I create voxel based terrain ? 2 Answers