x


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

more ▼

asked Jul 28 '11 at 05:07 PM

AlastorSparda gravatar image

AlastorSparda
16 3 3 4

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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);

}

}

more ▼

answered Jul 29 '11 at 01:19 PM

AlastorSparda gravatar image

AlastorSparda
16 3 3 4

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jul 28 '11 at 05:09 PM

DaveA gravatar image

DaveA
26.5k 151 171 256

Ok, i think i am doing something wrong, i used this and there is no error but nothing happend:

`var Suits : Material[]; var currentSuitIndex : int = 0; var currentSuit : Material;

function Start () { ActivateSuit(0); }

function ActivateSuit(index : int) { if (index >= Suits.Length) { index = 0; } currentSuitIndex = index; currentSuit = Suits[currentSuitIndex]; renderer.sharedMaterials[1] = currentSuit; }

function Update () { if (Input.GetKeyDown ("s")) { ActivateSuit(currentSuitIndex + 1); } }`

Jul 28 '11 at 06:08 PM AlastorSparda

Best to edit your question to put code there cuz you can format it as code there.

Jul 28 '11 at 08:16 PM DaveA

Assuming 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 sharedMaterial? http://unity3d.com/support/documentation/ScriptReference/Renderer-sharedMaterial.html

Jul 28 '11 at 08:19 PM DaveA
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x379
x274

asked: Jul 28 '11 at 05:07 PM

Seen: 1132 times

Last Updated: Jul 29 '11 at 01:20 PM