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;
}
}
[20116-screen+shot+2014-01-02+at+2.52.20+pm.png|20116]
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).
– Xenocide