I am doing a slingshot type game.The slingshot has two material. First material is only color.The second one is texture of the slingshot. I need to change the second one. var Sling : GameObject; var Material1 : Material; function OnMouseDown() { if(condition) { Sling.renderer.materials[1] = Material1; } } Also i tried the Sling.renderer.material = Material1; This only change the first material of Sling. Thanks..
The problem is that .materials is a property which returns a copy of the material array. All you can do is assign a new array to this property. To change the second material you would do something like:
var Sling : GameObject;
var Material1 : Material;
function OnMouseDown()
{
if(condition)
{
var mats = Sling.renderer.materials;
mats[1] = Material1;
Sling.renderer.materials = mats;
}
}
But be careful, this only works if there are two materials attached, otherwise you can’t just set another material. You would have to create a new, bigger array.
What is the exact configuration of your materials and renderers?
– syclamothI am doing a slingshot type game.The slingshot has two material. First material is only color.The second one is texture of the slingshot. I need to change the second one. var Sling : GameObject; var Material1 : Material; function OnMouseDown() { if(condition) { Sling.renderer.materials[1] = Material1; } } Also i tried the Sling.renderer.material = Material1; This only change the first material of Sling. Thanks..
– anon94900258same problem here :) is anybody solved it?
– DeveshPandeyBunny83 already posted the correct answer.
– Eric5h5