How to change the color of a label after the mouse hovers over a button?

Godot Version

4.2.1 stable

Question

I’m making a menu for my game, I need to make buttons with textures (which I prepared). For this I used TextureButton and Label, so I load the textures into normal, pressed and hover, I noticed that when you hover over the button, the text in the label looks bad and it could use changing the color to a darker one. I ask you to help show me a possible code for such an action (so that when you hover the mouse over the button, the color of the text on the label will change). Or would a different approach be suitable for my task instead of “TextureButton and Label”? I would also like to hear your opinion on this matter.

1 Like

Hello,
What i would try is putting an Area2D over the button and add a function that calls once you enter it? Basically being on the button…

Code:

var color_change: bool = false

func on_area2d_entered():
color_change = true
if color_change:
$Button.modulate(the color you want)

func on_area2d_exited():
color_change = false

I think this could work, but I’m not sure if this is all that efficient. Sorry if that is the case
Anyway, hope this kinda helps you

Buttons have the mouse_entered and mouse_exited signals. You can connect to these to detect when the mouse enters and exits the button. The code could look something like this:

func on_mouse_entered():
	$Label.modulate(Color.RED)


func on_mouse_exited():
	$Label.modulate(Color.WHITE)
4 Likes

Yeah, this is a way cleaner approach than mine… good job!