Change Color on Hover (Mouseover) – Unity3D (C#)


This function is used for UI buttons mostly but sometimes we need it in game specs. It is implemented easily with mouse enter-exit listeners. We can change color of an object with renderers. Create a cube object and attach this script to it.

At first, define 2 colors for standard and mouse over situations.

Next, we need a renderer.

Set the colour of cube object in start function.

Now we can write mouse over and mouse exit functions.

These 2 functions say that when you take mouse over the cube, make it hoverColor(red) and when you take off the mouse, make the cube gets its original color(green). Also you can add sounds instead of color.

Full code is below.

 

 

©Coffee Break Codes – Change Color on Hover (Mouseover) – Unity3D (C#)


Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “Change Color on Hover (Mouseover) – Unity3D (C#)