Search Unity

Changing materialss

Discussion in 'Scripting' started by MrZeker, Apr 21, 2019.

  1. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    Hello, im using a code to change the material when the model is swapped, sadly my new model has 2 materials, and i cant figure out how to change 2 materials at once. how can i accomplish that?.
    this is the code im using so far
    Code (csharp):
    1.  
    2.  
    3. if(MaiaBody == 1)
    4.         {
    5.             character.sharedMesh = MaiaBody001;
    6.             character.sharedMaterial = Maia001Skin;
    7.         }else
    8.  
    I need to add another material, Maia001bSkin, but i cant figure out how to enable the slot to set it.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Instead of `material', the mesh will have a `materials' array which you can change. You'll need to get a handle on the array, set the members, then reassign the mesh again. Something like this:

    Code (CSharp):
    1. var materials = character.materials;
    2. materials[0] = mat1;
    3. materials[1] = mat2;
    4. character.materials = materials;
     
    MrZeker likes this.
  3. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    Thanks man, worked like a charm!!!!!