Button Hover Sound Effect

Godot Version

V4.7

Question

Hi, I’m attempting to make the buttons play a sound when hovered over. I have the button set up in it’s own scene, with an AudioStreamPlayer as a child. I’m adding the button different scenes, and the button does work, but I’m getting an error (Image at the bottom).

I would like to note that I have assigned a sound to the AudioStreamPlayer, but the sound is not playing.

image

class_name AltButton
extends Button

@onready var button_hover_click: AudioStreamPlayer = $ButtonHoverClick

func _on_mouse_entered() -> void:
	button_hover_click.play()

image

Looks like your script is attached to some other node (see the node path in the error mesage)

I’m not sure what you mean.

I’ve slightly changed the names because I’m testing something out, but I’m still getting the same errors.

Here is the AnimatedButton.

image

class_name AnimatedButton
extends Button

@onready var button_hover_click: AudioStreamPlayer = $ButtonHoverClick

func _on_mouse_entered() -> void:
	button_hover_click.play()

Still the same error.

image

As I don’t see your AnimatedButton it in MainMenu, the path is be probably different the during runtime.

Could you run game and share scene_tree from Remote to where is your AnimatedButton instantiated ?

The Play, OptionsButton, etc. are the animated button.

Also, when I try renaming AnimatedButton, it again brings up the same error.

show us inspector section when it’s selected in scene_tree

also I don’t see your stream_player instantiated so it’s quite logical it’s won’t find it , as well as @normalized mentioned the script it’s not attached to that node.

this confirm the script it’s not attached to this node, because there is no export variable field to fill in.

how is this node getting instantiated? what is scene_tree for this?

What do you mean what is the scene tree?

I believe it’s getting instantiated by the script.

@onready var button_hover_click: AudioStreamPlayer2D = $ButtonHoverClick

ok so this is the scene_tree those two nodes.

How scene get instantiated? Is it child of level or some UI overlay?

the script it self is not instantiating it, you need to do it manually or via other code to instantiate the scene.

I don’t think that classes bring their children with them.

I don’t use classes a lot, sorry if it’s wrong.
You need to manually add the custom button or create an AudioStreamPlayer in the code.

I’ll try to answer the best I can. I’m sorry if I didn’t understand.

I implement the Animated Button with the Add Child Node. I make the AnimatedButton a child of the GridContainer, which is a child of the MainMenu node.

I’m guessing the code is not Instating the AudioSteamPlayer.

@onready var button_hover_click: AudioStreamPlayer2D = $ButtonHoverClick

so you instantiate class or scene?

code is only searching at start to have ready variable button_hover_click linked to $ButtonHoverClick which doesn’t exist because is not instantiated .

Solution

save this as scene and instantiate this scene under a child of the GridContainer .

They instantiated the class.

yes it’s strange if he instantiated node type( class) it definitely not showing attached script.

such as here.

Wondering that how they are instantiating the button.

me too as it looks like two unrelated scenes.

Okay, so, I’m adding the AnimatedButton like this

However, I decided to test by first removing it and clicking and dragging the buttons. (Renaming them.)

It plays a sound when I put my mouse over the button! IDK how though. There’s also no errors.

class_name AnimatedButton
extends Button

@onready var button_hover_click: AudioStreamPlayer = $ButtonHoverClick

func _on_mouse_entered() -> void:
	button_hover_click.play()

so it solved it right ?

?

Yeah, it’s solved now. Thank you.