Search Unity

Cannot dynamically change a material of a SkinnedMeshRenderer

Discussion in 'General Graphics' started by che1404, Jan 9, 2015.

  1. che1404

    che1404

    Joined:
    Jan 9, 2015
    Posts:
    5
    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:

    Code (CSharp):
    1. public class MaterialChanger : MonoBehaviour {
    2.  
    3.     public Material shieldMaterial;
    4.     private Material originalMaterial;
    5.  
    6.     void Start () {
    7.         originalMaterial = renderer.material;
    8.     }
    9.    
    10.     void Update () {
    11.         if (Input.anyKeyDown) {
    12.             if (renderer.material == originalMaterial)
    13.                 renderer.material = shieldMaterial;
    14.             else
    15.                 renderer.material = originalMaterial;
    16.         }
    17.     }
    18. }

    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.
     
  2. che1404

    che1404

    Joined:
    Jan 9, 2015
    Posts:
    5
    Anyone? :)

    Thanks!
     
  3. Ony

    Ony

    Joined:
    Apr 26, 2009
    Posts:
    1,977
    Does your skinned mesh have more than one material slot? If so then you have to assign the material to the correct slot, which you would do like this:

    renderer.materials[x] = shieldMaterial;

    Where 'x' = the slot for the material you are replacing. Notice the 's' at the end of 'material' and then of course the bracketed array slot.

    If your mesh only has one material then that won't help, but if it has multiple materials then you have to address it as an array of materials.
     
    Last edited: Jan 21, 2015
  4. che1404

    che1404

    Joined:
    Jan 9, 2015
    Posts:
    5
    Hi Ony, thanks for your reply.
    I ended up developing my own additive shader. The simplest one taken from the unity docs, and seems to work perfectly. There should be anything strange on the one distributed by Unity, but don't know what it is.

    Thanks anyway for your help.
    Robert.
     
  5. miragetech

    miragetech

    Joined:
    Sep 11, 2014
    Posts:
    1
    vr1209 likes this.