I am creating a simple game, where multiple prefab clones exist, and when the mouse is over a specific clone, the material color of the clone changes.
public Renderer[] childrenRenderers;
private Color startColor;
[SerializeField] private Material defaultMaterial;
[SerializeField] private Material transparentMat;
// Use this for initialization
void Start () {
GetComponents();
}
void GetComponents (){
childrenRenderers = GetComponentsInChildren<Renderer> ();
foreach(Renderer rend in childrenRenderers){
rend.material = setDefaultMat();
}
//both materials have the same color, so we chooce randomly the first.
//startColor is the default material color that the turbine has.
startColor = defaultMaterial.color;
}
public void UndamagedHighlightMat (){
foreach(Renderer rend in childrenRenderers){
rend.material = setDefaultMat();
//rend.material.color = Color.cyan;
rend.material.SetColor("_Color",Color.cyan);
}
}
public void UndamagedDefaultMat (){
foreach(Renderer rend in childrenRenderers){
rend.material = setDefaultMat();
rend.material.color = startColor;
}
}
public Material setDefaultMat(){
return defaultMaterial;
}
When the mouse is over the UndamagedHighlightMat () is called and when not the UndamagedDefaultMat ().
I have tried many things like using sharedMaterial, but that changed every clone’s material and not the specific one I wanted.
Also, I found out that the problem was starting when the color was changing because the total number of objects in the profiler was increasing every time the material color changed.
So any help on the topic will be greatly appreciated, thank you in advance.