Accessing second material

Hi all,

I am using SetTexture to change a material’s texture at runtime and everything is working great. My problem is that some of my models have multiple materials and it is not always the first material’s texture i need to change.

How can I access a model’s second material or third etc and use SetTexture to change them?

Btw, I’m coding in C#

Many thanks in advance,
Matt.

A model with multiple materials will have an array in the Inspector. Just use the appropriate index in renderer.materials with your SetTexture.

220369--7997--$screen_shot_2009_11_07_at_25957_pm_503.png

1 Like

Thanks Jessy and sorry for even asking the question… i looked in the scripting reference and didn’t even see renderer.materials! My fault, i was using renderer.material

Thanks again for pointing me to it.

Regards,
Matt.

Hi Jessi can you show me an example? I’m trying but does not work for me!

Blast from the past.

If you definitely have more than one material on an object, you could use:

renderer.materials[1] to get the second material for example. If that doesn’t work you’re doing something else wrong, if that’s the case could you post what error you’re getting?

2 Likes

See the examples in the script reference.