- Home /
Question by
kevinforrestconnors · Dec 18, 2017 at 04:06 PM ·
texturemeshmaterial
Applying a material to mesh parts
I'm trying to apply a material to all the meshParts of a GameObject. Here is my code:
Make a new material:
Material landscapeMaterial = new Material(Shader.Find("Standard"));
Fetch the texture:
Texture landscapeTex = (Texture)AssetDatabase.LoadAssetAtPath("Assets/landscape_texture.tiff", typeof(Texture));
Set the texture of the material to be the texture that was fetched:
landscapeMaterial.mainTexture = landscapeTex;
For all meshParts:
foreach (Transform meshPart in def) {
Renderer rend = meshPart.GetComponent<Renderer>();
rend.material = landscapeMaterial;
}
Comment
Answer by kevinforrestconnors · Dec 18, 2017 at 04:35 PM
This is the line I was missing!
AssetDatabase.CreateAsset(landscapeMaterial, "Assets/Materials/landscape.mat");
Your answer
![](https://koobas.hobune.stream/wayback/20220612144816im_/https://answers.unity.com/themes/thub/images/avi.jpg)