- Home /
Changing Material between multiple ones
I have a character with 3 materials, and i want to change it by code, i already have a script to change a material, is there a way to specify which material from my object i want to change?
Thanks
Answer by AlastorSparda · Jul 29, 2011 at 01:19 PM
Yup i did, but there seemed to be a bigger problem, i share with you my solution... i had to firt take the materials my mesh already had and save it on a var...
var Suits : Material[];
var Detail : Material[];
var currentSuitIndex : int = 0;
var currentDetailIndex : int = 0;
var currentSuit : Material;
var currentDetail : Material;
var currentSuitMat : Material[];
var ColorSuit : Color[] = [Color.red, Color.white, Color.blue, Color.green, Color.yellow, Color.black, Color.cyan, Color.magenta, Color.gray];
var currentSuitColorIndex : int = 0;
var currentDetailColorIndex : int = 0;
function Start ()
{
currentSuitMat = renderer.materials;
ActivateSuit(0);
ChangeSuitColor(0);
ChangeDetailColor(0);
}
function ActivateSuit(index : int)
{
if (index >= Suits.Length)
{
index = 0;
}
currentSuitIndex = index;
currentDetailIndex = index;
currentSuit = Suits[currentSuitIndex];
currentDetail = Detail[currentDetailIndex];
currentSuitMat[1] = currentSuit;
currentSuitMat[2] = currentDetail;
renderer.materials = currentSuitMat;
}
function ChangeSuitColor(index : int)
{
if (index >= ColorSuit.Length)
{
index = 0;
}
currentSuitColorIndex = index;
currentSuitMat[1].SetColor("_Color", ColorSuit[index]);
renderer.materials = currentSuitMat;
}
function ChangeDetailColor(index : int)
{
if (index >= ColorSuit.Length)
{
index = 0;
}
currentDetailColorIndex = index;
currentSuitMat[2].SetColor("_Color", ColorSuit[index]);
renderer.materials = currentSuitMat;
}
function Update ()
{
if (Input.GetKeyDown ("s"))
{
ActivateSuit(currentSuitIndex + 1);
}
else if (Input.GetKeyDown ("d"))
{
ChangeSuitColor(currentSuitColorIndex + 1);
}
else if (Input.GetKeyDown ("f"))
{
ChangeDetailColor(currentDetailColorIndex + 1);
}
}
Answer by DaveA · Jul 28, 2011 at 05:09 PM
Yes you find the object within your character that has the material. If there is an array of materials, it will be indexed like any other array of things.
Ok, i think i am doing something wrong, i used this and there is no error but nothing happend:
var Suits : $$anonymous$$aterial[]; var currentSuitIndex : int = 0; var currentSuit : $$anonymous$$aterial; function Start () { ActivateSuit(0); } function ActivateSuit(index : int) { if (index >= Suits.Length) { index = 0; } currentSuitIndex = index; currentSuit = Suits[currentSuitIndex]; renderer.shared$$anonymous$$aterials[1] = currentSuit; } function Update () { if (Input.Get$$anonymous$$eyDown ("s")) { ActivateSuit(currentSuitIndex + 1); } }
Best to edit your question to put code there cuz you can format it as code there.
Assu$$anonymous$$g the 'suit' mesh is at index 1, this looks ok to me. Of course you have assigned the materials to the Suits array, right? Are you sure you want shared$$anonymous$$aterial? http://unity3d.com/support/documentation/ScriptReference/Renderer-shared$$anonymous$$aterial.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612161322im_/https://answers.unity.com/themes/thub/images/avi.jpg)