Why isn't my audio playing when the button is pressed?

Godot 4.3

I have been working on a project and I decided to load some audio in for the main menu sounds when a button is pressed. But it doesn’t seem to recognize “play()” or “amp” somehow (even though the variable is AMP). How can i make audio play when the button is pressed?

CODE:
extends Button

@onready var audio : AudioStreamPlayer = $AudioStreamPlayer
@onready var amp = get_node(“AudioStreamPlayer”)

func _on_pressed():
get_tree().change_scene_to_file(“res://scenes/menu screens/screens/game_modes.tscn”)
amp.play(“buttonpress”)

If anyone could help me with this issue it would be greatly appreciated.

Hi,

You have changed scenes before calling the play sound. So the sound player no longer exists, so no sound will play.

You could play the sound before changing scene and put a timeout to allow it to finish before changing, or add a singleton / global script with the sound player, so it’s not lost when changing scenes.

Ryn

1 Like