How to access second material in object?

I’m trying to change the second material in all objects in my array. I’m currently using

for (var i = 0; i < structure.Length; i += 1)
        {
            structure[i].GetComponent<Renderer>().material = mats[0];
        }

This only changes the first material but I want it to change the second. When I do:

for (var i = 0; i < structure.Length; i += 1)
        {
            structure[i].GetComponent<Renderer>().material[1] = mats[0];
        }

I get an error. Does anyone have any ideas? I saw online someone said do render.material[1] but I don’t know how to make that work in the current code I have.

1 Like

sharedMaterials is probably the one you want to use.

materials.

https://docs.unity3d.com/ScriptReference/Renderer-materials.html

Materials will clone material so it depends on what you wanna do. If you want to use same material (benefit from batching etc) you need to use sharedMaterials, otherwise you end up with a clone.

If you change the sharedMaterial it will change for all, so it depends on what you want todo.