- Home /
Access the nth materials in the renderer
Hello, I want to ask if there is a way to change only one material in a gameobject. My gameobject's renderer has 4 materials inside but I only want to access one material only so i can change its color. the material is named "10" inside element 2 (the yellow one). Please help me.
Answer by hexagonius · Sep 18, 2017 at 05:52 AM
Yes. Get the renderer, get either the materials array or the sharedMaterials array. Change the materiel at index 2 and assign it back.
Access materials only if you need an individual change, use shared otherwise.
Thank you! I found the way to do it based on your suggestion.
You actually don't have to assign the array back unless you want to replace the material. Changing the material can be done "directly". The materials are "referenced" by the renderer. So once you have a reference to one particular material you can modify that material, however you can't replace a single material. To replace one you have to assign the array back.
ps: If you actually never replace any material but just want to change some properties on them you may want to store a seperate reference in your script in Start to avoid the creation of the array each time you want to access the material.
Your answer
![](https://koobas.hobune.stream/wayback/20220612135105im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
Changing specific materials onTriggerEnter 3 Answers
Gameobject not the right color 0 Answers