I’m trying to change the shader of a material at runtime. The shader (outlineShader) is set in the inspector as a public variable. That part works, but I want to keep the original main texture but that part doesn’t work. The texture is blank on the object.
//get the current main texture
Texture tex = t.gameObject.renderer.material.mainTexture;
//change to the new shader and set the texture
Material myNewMaterial = new Material( outlineShader );
myNewMaterial.mainTexture = tex;
t.gameObject.renderer.material = myNewMaterial;
I’ve also tried this with the same result
Texture tex = t.gameObject.renderer.material.mainTexture;
t.gameObject.renderer.material.mainTexture = tex;
t.gameObject.renderer.material.shader = outlineShader;
I’ve also tried setting the shader 1st and the texture second
Texture tex = t.gameObject.renderer.material.mainTexture;
t.gameObject.renderer.material.shader = outlineShader;
t.gameObject.renderer.material.mainTexture = tex;