- Home /
Altering Material through Script Causes Transparent Rendering Error - C#
I simply need to swap a material on an object with multiple materials. If I run a for loop like so:
public Material newMat;
myMats = render.sharedMaterials
for(int i = 0; i < myMats.length; i++) {
if(myMats[i].someProperty == someCheck)
{
myMats[i] = new Material(newMat);
}
}
... or any variation of this, the Material simply doesn't change.
Alternatively, I thought I could use something like this, changing the material's properties, rather than the material altogether:
if(myMats[i].someProperty == someCheck)
{
myMats[i].shader = newMat.shader;
myMats[i].CopyPropertiesFromMaterial(newMat);
}
... This almost does exactly what I need, but if the material changes from an opaque rendering mode to a fade, for example, all of the properties change, but the material doesn't render properly until I manually change something in the inspector (for example, nudging the color bar). After I touch the material manually, it corrects itself and renders as it should. This isn't helpful though, as it should be automatically updated in game.
Is this a known Unity bug, or is there a way to update/refresh the shader after applying changes? Any feedback is appreciated. Here are some screenshots to better explain my problem. The first is the material before the change, the second is how it SHOULD look after the change, but the third is my actual result.
Answer by Bunny83 · Jul 09, 2015 at 09:33 AM
Uhm, where do you actually assign the material(s) to your renderer(s)? "myMats" seems to be an array of Material references. When you exchange a material in that array, the material that your renderer uses isn't changed.
If you use the ".materials" or ".sharedMaterials" array property of your renderer, you get a new array containing the used material. If you want to change them you have to assign the array back:
Material[] myMats = renderer.materials;
// do your changes
renderer.materials = myMats;
Keep in mind that when using ".materials" or ".material" Unity will create a copy of each material just for this instance. This might be relevant for drawcalls. The "sharedMaterial(s)" contain direct references to the actual material(s). However exchanging a material in the sharedMaterials array has to be done the same way:
Material[] myMats = renderer.sharedMaterials;
// do your changes
renderer.sharedMaterials = myMats;
ps: about "CopyPropertiesFrom$$anonymous$$aterial", this might be some kind of bug in that method. $$anonymous$$aybe they haven't considered the new default shader and some settings are lost.
Thanks for the reply! Unfortunately, it didn't solve my problem.
my$$anonymous$$ats = renderer.shared$$anonymous$$aterials. Therefore, the changes appear to be automatically applied to the renderer. I did implement your suggestion though, hoping for different results, but I get the same result no matter which scripting path I take. I've attempted a material swap in the simplest form possible, with one object and two materials, as you see here, but it happens either way.
I'm pretty confident this is a bug in Unity 5, because I've been doing material swaps since, literally, my first Unity game three years ago, and I've never come across this issue. Still hoping I'm wrong and simply overlooking something, though.
Your answer
Follow this Question
Related Questions
Distortion Shader does not render transparent objects (Shader Graph) 2 Answers
Sequential shaders. Pass the output color of a shader to the next one. 0 Answers
set gameobject in front of all others and don't have own faces appear in front of each other 1 Answer
Transparency cutting opaque mesh 0 Answers
Cheap soft particles for mobile 0 Answers