Search Unity

material, sharedMaterial sharedMaterials confusion

Discussion in 'Scripting' started by metervara, May 1, 2007.

  1. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    This has been discussed in other threads but I'm still confused after reading them through so bear with me.

    The question is how do I change the material instances on a GO with multiple materials? E.g changing Material2 to a different material for this instance:



    Impossible?

    renderer.material does it to the first material of the renderer, in this case Material1. Doing something like renderer.sharedMaterial.color = Color.red changes all the instances of Material1, and renderer.sharedMaterials[1].color = Color.red changes all instances of Material2. Assigning a new material like this renderer.sharedMaterial = someMaterial works, but doesn't seem to have any effect if used with sharedMaterials -> renderer.sharedMaterials[1] = someMaterial doesn't work.

    this thread suggests instantiating a copy of sharedMaterial[1], editing and reassigning, but since renderer.sharedMaterials[1] = someMaterial doesn't work I can't see how that would do it.

    And the materials property of the mesh renderer component is not accessible from scripting.

    Is there really no way to replace Material2 in the example above without changing the shared material aswell?

    thanks - Patrik
     
  2. Talzor

    Talzor

    Joined:
    May 30, 2006
    Posts:
    197
    You can't reassign a single element of sharedMaterials, you have to reassign the entire array.
    Code (csharp):
    1.  
    2. Material[] sharedMaterialsCopy = r.sharedMaterials;
    3. sharedMaterialsCopy[1] = someMaterial;
    4. r.sharedMaterials = sharedMaterialsCopy;
    5.  
     
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    and you can use Instantiate to create a duplicate of the material.
     
  4. Talzor

    Talzor

    Joined:
    May 30, 2006
    Posts:
    197
    Small extra clarification:
    r.sharedMaterials[someNumber] = someMaterial doesn't work because sharedMaterials doesn't return an reference to the renderers shared materials, but instead returns a new array containing the references to the shared materials. Thus assigning anything to this array does nothing as it immediatly goes out of scope.
     
    bion, theolagendijk and efge like this.
  5. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    Great! Thanks a bunch!

    /P
     
  6. sidvangala26

    sidvangala26

    Joined:
    Aug 30, 2017
    Posts:
    2
    I tried the same

    Code (CSharp):
    1. Material[] DressMaterialsCopy = Body.sharedMaterials;
    2.                     DressMaterialsCopy[1].color = selectedColor.color;
    3.                     Body.sharedMaterials = DressMaterialsCopy;
    But when I change scenes, the material is getting reset to its original value.

    This happens only in the build (Android) but works in the unity editor..What am I doing wrong?