- Home /
Change material per element
I have made a car in 3ds Max with about 11 different materials. I want to change most of the materials in code. But I can only get to changte "Element 0". I'd like to be able to change (for example) "Element 4" as wel. How can I make this work???
With this code I'm able to change "Element 0" by clicking 3 different buttons. Every button changes the material of "Element 0" to another material.
var Car : GameObject;
var CarPaint1 : Material;
var CarPaint2 : Material;
var CarPaint3 : Material;
function OnGUI()
{
if(GUI.Button(Rect(0,0,100,50),"Blauw"))
{
Car.renderer.material = CarPaint1;
}
if(GUI.Button(Rect(0,50,100,50),"Oranje"))
{
Car.renderer.material = CarPaint2;
}
if(GUI.Button(Rect(0,100,100,50),"Zwart"))
{
Car.renderer.material = CarPaint3;
}
}
Answer by whydoidoit · Jun 22, 2012 at 02:37 PM
You need to be using Car.renderer.materials which is an array of the materials.
Okay. And how can i define which Element it changes? With [0],[1] etc or something?. Cause that didn't seem to work for me :S
As stated in the documentation, Car.renderer.materials returns a copy of the materials array, not a reference. $$anonymous$$ake sure to store this copy locally, make any changes you want, then set the entire array back all at once.
Okay, that seems like the right way to do it. But how do I make this happen? I'm sort of a noob (A)
Your answer
Follow this Question
Related Questions
change a material from multiple materials 1 Answer
How to change the colour of my car? 1 Answer
change materials onmousedrag 2 Answers
change skybox via script help ? 1 Answer