Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity 5.0b9 SharedMaterials

Discussion in 'Unity 5 Pre-order Beta' started by jordi_boni, Nov 3, 2014.

  1. jordi_boni

    jordi_boni

    Joined:
    Feb 7, 2013
    Posts:
    18
    I am developing a camouflage system and I change sharedmaterials in a c# script, but the renderer don't use the new material.

    This is my source code:
    Code (CSharp):
    1. meshRenderer.sharedMaterials[i].color = Color.red; // OK, it changes the color
    2. meshRenderer.sharedMaterials[i] = myNewMaterial; //FAIL
    Sharedmaterials changed in Unity 5 or it's a new bug?
     
  2. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    I'm not sure, but shouldn't you assing meshRenderer.materials? Since sharedMaterials is a copy anyway.
    From the docs:
    "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."

    So, use:
    Code (csharp):
    1. Material[] mats=meshRenderer.sharedMaterials;
    2. mats[i]=myNewMaterial;
    3. meshRenderer.sharedMaterials=mats;
     
  3. jordi_boni

    jordi_boni

    Joined:
    Feb 7, 2013
    Posts:
    18
    It works fine, thx!