Not a bug, thats expected. Same also happens with transform.rotation and transform.position for example.
in UnityScript its not required, in C# its a consequence of what it does under the hood. You must store it in a temp reference to modify it, you can’t modify the result array directly as it is not fed back in.
Confirmed: The same workaround works for UnityScript, but still seems a non sense behavior, also undocumented…
NOT REAL CODE (simplified from mine):
var newMaterial = new myComponent.SkinnedMeshRenderer.materials ;
newMaterials[0] = someMaterial;
newMaterials[1] = someOtherMaterial;
newMaterials[2] = yetAnotherMaterial;
myComponent.SkinnedMeshRenderer.materials = newMaterial;
Just to clarify, since this isn’t anywhere in the docs and it could be very confusing for new users trying to figure out how Unity works:
When you call renderer.material, it actually makes a copy of the material, so if you make changes to the material while the game is running, the copy is changed and not the original material in your assets folders.
If you call renderer.materials, it does the same thing, but it makes a copy of the entire materials array. So if you then assign to that array, you’re assigning to the copy, but that copy is no longer linked to the original renderer.
So, as Dreamora said, you have to know this, and if you do, you can work around it. You call renderer.materials, which makes a copy of the array, and you save that copy to a variable. You then do your changes on the copy. Finally, you assign the copy back to the renderer, with renderer.materials = myCopy (or whatever your variable is called).
Hardcore Games proposed an alternative method, but keep in mind that what he’s actually doing is copying properties from one material to another, not updating the material reference. If you have 2 different materials with 2 different shaders, his example won’t do what you think it should.
Actually, it’s working exactly as expected. I’m switching between two shaders, one is opaque, one is transparent, and his suggestion is working just fine.
I agree that this should be made more clear in the documentation because trying to set one material to another if it’s part of a Materials array isn’t very intuitive.
Note that like all arrays returned by Unity, this returns a copy of materials array. If you want to change some materials in it, get the value, change an entry and set materials back.
That basically tells you EXACTLY how to do it: copy the array out, change the one you want, and copy the array back.
Since I clearly see documentation about this, wondering if this is something modified in the documentation, since you posted your issue?? Or has this documentation always been there, and you missed it?? Or maybe you didn’t understand it, since it isn’t very clear??
Doc says: “Note that like all arrays returned by Unity, this returns a copy of materials array. If you want to change some materials in it, get the value, change an entry and set materials back.”
Might not be totally clear, as stated, but still documented! This is basically saying what others have said about how to properly change multiple materials on the same renderer! you can’t just directly assign new values to the array elements. But maybe your question is WHY you can’t do it this way?? Still fairly new to C#, so not sure about those details. I just know that “this is how you do it”!
The point is that getting the property with the lil line of code i previously posted raises an error.
Just try it yourself and you’ll see it
IMHO this help page needs a simple code example for usage of this property.
However, as unity is always growing and as the doc is a huge ( at least as big as unity3D itself ) project, document team might have many other things to do^^
This caused me a lot of time spent debugging! I agree the current solution is a anti-pattern. And the engineered behavior is bug-like, requiring extra steps that are easily forgotten and make identifying problems in the code difficult.
When I set a material: renderer.materials[Idx] = newMmaterial;
there is no error but the materials are left unchanged.
In comparison if I set a single axis: transform.rotation.x = 1;
the compiler shows a error that the returned array is temporary.
Despite both being a similar issue, the prior case goes unchecked…