- Home /
How to change all materials of object and then change it back to normal?
I have an object which i made in blender. The object has got different materials. What I want is that when i hold the mouse on the object, it changes it's color (maybe adding a mask on it would be perfect). And when the mouse exit on object, it should change back to original colors. I have searched for other questions but I haven't find a solution for objects with different materials.
Renderer objRenderer;
Color[] originalColor;
void Start()
{
objRenderer = this.GetComponent<Renderer>();
originalColor = new Color[objRenderer.materials.Length];
foreach (Material mat in objRenderer.materials)
{
for (int i = 0; i < objRenderer.materials.Length; i++)
{
originalColor[i] = mat.color;
}
}
}
void Update()
{
if (selected == true)
{
foreach (Material mat in objRenderer.materials)
mat.color = Color.red;
}
else
{
foreach (Material mat in objRenderer.materials)
{
for (int i = 0; i < objRenderer.materials.Length; i++)
{
mat.color = originalColor[i];
}
}
}
}
Comment