I'm writing a simple C# Script to change Materials color of a object.
public class Highlight : MonoBehaviour {
public bool Mouse_Over = false;
public Color Highlight_Color = Color.white;
private Renderer rend;
private Material[] mats;
// Use this for initialization
void Start () {
rend = this.GetComponentInChildren< Renderer >();
mats = rend.materials;
}
public void ToggleHighlight(bool t){
if (mats.Length == 0) {
return;
}
for(int i=0; i < mats.Length; i++){
if (t) {
rend.materials[i].color = Highlight_Color;
} else {
rend.materials[i].color = mats[i].color;
}
Debug.Log ("Material_" + i + ".Color = " + mats[i].color.ToHexStringRGBA());
}
}
void OnMouseEnter(){
if (Mouse_Over) {
ToggleHighlight (true);
Debug.Log ("MouseEnter");
}
}
void OnMouseExit(){
if (Mouse_Over) {
ToggleHighlight (false);
Debug.Log ("MouseExit");
}
}
}
As you can see, I store my original Materials inside the mats
array, in the Start
function.
This is to revert the renderer Materials to the original Materials, but it isn't working. The color doesn't change to the original value.
I tried to check where the problem was, and in the Debug statement inside the for loop, the color is getting updated also inside mats
Array.
Why is this happening?
Seems like that mats
is only a pointer to rend.materials
, am I wrong?