The reason I am here is becuase I couldn’t find anything on this anywhere else, its probebly alot about it but I couldt fint anything I understood or that could help me out. So I hope someone here can help me, and explain how I can do this whitout just trowing me some links.
I want to create an object that can have multiple materials on an single mesh, at the same time where I am able to turn some of them off and on through script. It is very important that all of the materials is showing, and not just replaced whit the old one, since many of them are transparent and needs to be over other materials.
I came to understand that the “material size” on the mesh of the object lets me add multiple materials, which works just the way I want them to, since I can clearly see they are all appering on top of each other like I want.
The only problame is, how can I turn them on, and off in the script? I dont need to change them, becuase they are supposed to be what they are, but I just want to turn some of them off and on at any given time!
I hope someone can help me out,
Thanks in advance,
Best Regards,
Winsj
when you have more than one material, the material attribute of the renderer is an indexed array. so instead of renderer.material you would access renderer.material[n]
Since the render is what you would turn on and off (enable), to turn on/off a single material you would either:
a) remove it to turn it off, add it to turn it on, or
b) set the alpha (depends on the shader you use) to turn it off/on
I dont use any shaders, since I have little experiance whit it and read something about this very way of doing it would work fine whitout it and would safe performance!
Would you mind explaining to me how I should go about turning it on/off whit the script? Maybe link me to the correct script reference for the right method to use?
The “renderer.material[n]”, would the N be the name of the material I want it to render whit? So, I could multiply this line to render more then one? and if so, how can I turn it off again?
All materials have shaders, but the parameters and how they affect things may differ. For example, simply changing the alpha on a simple diffuse shader won’t make the material transparent.
I usually use Transparent/VertexLit if I need a simple object to fade. But for simple on and off, you could just set it to null.
The “n” refers to the index number of the mat. so the first one would be material[0].
There really isn’t a reference script I can point to, because it just depends on what you are using to determine which mats are being turned on and off and what triggers it.
What I would probably do is have the gameobject on start read all the mats and store them in an array for reference. So turn off the second mat you would have in your method:
renderer.material[1] = null;
to turn it on:
renderer.material[1] = ref_array_name[1];