- Home /
Changing textures on multiple materials shared by a mesh renderer
Hello. I am working on a project and using JavaScript to change the main textures on materials at the push of a GUI button (renderer.material.mainTexture= whatever). I've been keeping it really simple and so far, things have been moving along perfectly and without any trouble.
However, I just encountered a mesh renderer on a single mesh that is using two separate materials. I apply my texture switcher script and just as I expected, it is only switching the texture on one of the two materials.
I'm attaching the script I am using as well as a screen grab of my inspector. Again, this is working perfectly on everything else except the mesh with 2 materials. Is there a way to change the texture on that second material?
var RaceRedTexture:Texture; var SilverMetallicTexture:Texture; var DarkBlueTexture:Texture; var FrozenWhiteTexture:Texture; var PantherBlackTexture:Texture; var TectonicSilverTexture:Texture; var SchoolbusYellowTexture:Texture; var MidnightSkyTexture:Texture; var BurnishedGlowTexture:Texture; var SolarYellowTexture:Texture; var DeepImpactBlueTexture:Texture;
function AssignColor (chg:int)
{ switch(chg) { case 1:// Race Red gameObject.renderer.material.mainTexture=RaceRedTexture; break;
case 2:// Silver Metallic
gameObject.renderer.material.mainTexture=SilverMetallicTexture;
break;
case 3:// Dark Blue
gameObject.renderer.material.mainTexture=DarkBlueTexture;
break;
case 4:// Frozen White
gameObject.renderer.material.mainTexture=FrozenWhiteTexture;
break;
case 5:// Panther Black
gameObject.renderer.material.mainTexture=PantherBlackTexture;
break;
case 6:// Tectonic Silver
gameObject.renderer.material.mainTexture=TectonicSilverTexture;
break;
case 7:// Schoolbus Yella
gameObject.renderer.material.mainTexture=SchoolbusYellowTexture;
break;
case 8:// Midnight Sky
gameObject.renderer.material.mainTexture=MidnightSkyTexture;
break;
case 9:// Burnished Glow
gameObject.renderer.material.mainTexture=BurnishedGlowTexture;
break;
case 10:// Solar Yellow
gameObject.renderer.material.mainTexture=SolarYellowTexture;
break;
case 11:// Deep Impact Blue
gameObject.renderer.material.mainTexture=DeepImpactBlueTexture;
break;
}
}
Answer by shockreel · Jan 03, 2014 at 04:25 PM
I answered my own question. It took some digging to find but it was super simple. Multiple materials on a mesh are handled in Unity as an array. Here's the solution for anyone who needs it: (I'm just posting a small chunk of my code.)
function AssignColor (chg:int)
{
switch(chg)
{
case 1:// Race Red
gameObject.renderer.materials[0].mainTexture=RaceRedTexture1;
gameObject.renderer.materials[1].mainTexture=RaceRedTexture2;
break;
}
}
Anyone know how to do this for GetComponent().material? I have two materials in my renderer. How do I specify the other one when all it allows me to do is choose the first instantiated??
I can't find an answer. I usually figure it out while I post a problem so I'll update here if I figure it out.
EDIT: Okay it was GetComponent().materials to be able to choose which material. So GetComponent().materials[1] will choose my second element (as in second material), [0] will choose the first element (first material).
Your answer
Follow this Question
Related Questions
Texture doesn't get applied on .FBX file 3 Answers
Why does applying a material to a object break batching? 2 Answers
Unity Models Question 0 Answers
Gray line at top of the texture 0 Answers
Blender sphere object not rendering materials correctly in unity 1 Answer