- Home /
Changing shaders of a GameObject via script
Hi , I have a gameobject that have 5 materials with diffuse shaders. I want to changing the shaders of all from diffuse to Trasparent/Diffuse. By bellow code i can only change one shader. how can i access all shaders and change them? maybe with using an array.
var shader1 : Shader;
var shader2 : Shader;
function Start(){
shader1 = Shader.Find( "Diffuse" );
shader2 = Shader.Find( "Transparent/Diffuse" );
}
function Update() {
if (Input.GetKeyDown("f")) {
if( renderer.material.shader == shader1 )
renderer.material.shader = shader2;
}
}
Yes, use an array. Lots of non-Unity places explain them, and also plenty here. $$anonymous$$aybe not for shaders, but same idea.
But, shaders tend to also have settings you want to keep, or multiple textures, $$anonymous$$ight be better to swap $$anonymous$$aterials, where each $$anonymous$$at has the shader you want, plus everything set the right way.
Answer by asafsitner · Dec 23, 2012 at 11:26 PM
You can access the renderer's `materials` property which holds all the materials, then change them one by one.
Your answer
Follow this Question
Related Questions
Modifying a shader color dynamicly through another script 0 Answers
How to make a model to seems like it is rotating very fast with out rotating it fast 1 Answer
Change AI Follow Players 1 Answer
How to add transparency to a shader!? 1 Answer
Errors while assigning material to other object's by scripting. 1 Answer