- Home /
Adding materials at runtime or disabling one of multiple materials?
I use a cube as terrain and it has a material with my map. I would like to implement an option whereas the player can choose to highlight certain terrain features (like cities, for example). I thought that maybe I could use multiple materials - a base one with the map and then each with the different features highlighted. This way, the player could add only those that they wanted to the MeshRenderer materials array. I used the following line to load the material representing cities but it did not work - when I attached Visual Studio to Unity, I could see "materials[2]" change in VisualStudio to "mapCitiesMaterial" but the material in the Inspector did not change. If my code wrong or is this approach incorrect?
map.GetComponent().materials[2] = mapCitiesMaterial;
Your answer
![](https://koobas.hobune.stream/wayback/20220613060241im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mesh renderer with three submesh textures - only one renders properly, others are solid colors 1 Answer
How to change a specific material of an object ? 0 Answers
Renderer with multiple materials -- per-material MaterialPropertyBlocks? 1 Answer
Material vs. SharedMaterial - difference in SETTING? 1 Answer
Getting mesh material length 1 Answer