- Home /
Cannot dynamically change a material of a SkinnedMeshRenderer
Hi all, I have an animated character in my game that takes a shield component and I'm trying to change from the original material (Mobile/Unlit), to the new shield material (Mobile/Particles/Additive). I'm doing it like this:
public class MaterialChanger : MonoBehaviour {
public Material shieldMaterial;
private Material originalMaterial;
void Start () {
originalMaterial = renderer.material;
}
void Update () {
if (Input.anyKeyDown) {
if (renderer.material == originalMaterial)
renderer.material = shieldMaterial;
else
renderer.material = originalMaterial;
}
}
}
I have isolated the problem into a simple scene, containing my character (with a SkinnedMeshRenderer component), and a simple cube (with a MeshRenderer component), and I have attached the script to both of them.
Well, the material update only works for the cube, but not for the character. The material is stored in an Assets/Materials folder, and I have just dragged it to the script via unity interface.
I don't know what I'm doing wrong, but the truth is that it randomly works in the Editor, sometimes only in Android, and some others only in iOS.
My guess is that there is a fatal combination between SkinnedMeshRenderer and this particular shader (Mobile/Particles/Additive), because changing to a Mobile/Unlit works perfectly.
I have dedicated a lot of hours to this issue, and I'm assuming I won't be able to use this shader in the game. Any ideas? Any other test I could make to discard options? Thanks in advance, Robert.
Answer by Paulius-Liekis · Jan 09, 2015 at 06:31 PM
Note that renderer.material
the first time you call it returns a copy of a material. I'm not entirely sure what happens when you assign renderer.material
and then get it again. Try changing it to renderer.sharedMaterial. Anyway, it should not lead to different behaviour on different platforms.
Your answer
Follow this Question
Related Questions
How to change color of Particles/additive material ? 1 Answer
Change material color at runtime!! 5 Answers
Creating a mesh and assigning a material during edit time. 1 Answer
Bullet Texture , Material and Glow Effect : Unity 1 Answer
How to trigger an effect when an object is under a shadow? 0 Answers