Hi,
I’m trying to create a terrain through code, and so far everything works well, but I can’t find how to assign a material to the terrain. I’d like to assign the default-diffuse material.
I tried this:
terrainObj.GetComponent<Terrain>().materialType = Terrain.MaterialType.BuiltInStandard;
But it does not work, I still get a fully pink terrain.
Any ideas? 
This seems to work:
using UnityEngine;
public class SetTerrainMaterial : MonoBehaviour
{
public Material material;
void Start()
{
GetComponent<Terrain>().materialTemplate = material; ;
}
}
Thanks for you answer,
I tried that already, and it doesn’t work, but further investigation makes me think that the terrain generated through my script is somehow “incomplete”, note made correctly, because, for example, I can’t paint textures on it. There probably is something that I forget setting or initializing.
Odd. It works for me. Here’s a before and after picture, where I set it to a material I created with a red color:

Try it on a terrain you create with the editor. Perhaps you are right and it is something about how you create your terrain in code that is causing the problem.
It works! I found what was wrong in my code. Thanks for your insights!
1 Like