- Home /
Changing the tint colour of materials
EDIT: I have now got the script working but it's makes the material a instance, and doesn't change all the meshes with this material on.
So i'm trying to achieve something which I thought'd be really simple but turns out to be very difficult. My game object has 4 seperate materials attached to different sections. I want to be able to change the tint colour of each or replace the material to another preset through UI. Here's in code what i basically want it to do :
#pragma strict
var rend : Renderer;
var Blinn1 : Material;
var Blinn2 : Material;
var Blinn3 : Material;
var Blinn4 : Material;
function Start () {
}
function Update () {
}
function MatYellow(){
rend.material.color = Color(0, 0, 0);
Debug.Log("yellow");
}
function MatRed(){
Debug.Log("red");
}
So... what's the problem? You need to explain the difference between the expected behaviour and the observed behaviour before we can make a diagnosis.
The script is being called, but can't figure out how to actual make it replace the existing material, i think the problem is in gameObject.GetComponent.().materials[0] = $$anonymous$$at_yellow;, i'm unsure on the correct way to do this. Also I'm unsure on how to do this on an objcet which has more than 1 material attached to different sections of the same mesh.
Answer by FortisVenaliter · Apr 13, 2017 at 05:14 PM
For MeshRenderers with only one material:
renderer.sharedMaterial = newMaterial;
For MeshRenderers with multiple materials:
Material[] mats = renderer.sharedMaterials;
mats[i] = newMaterial;
renderer.sharedMaterials = mats;
And in reply to your new edit:
When you use Renderer.material, it instantiates a new one.
When you use Renderer.shared$$anonymous$$aterial, it applies to all.