AudioStreamPlayer plays just sometimes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tautologias345

A pressed() signal on a button has this code:

if get_tree().current_scene.filename == "res://Scene1.tscn":
	$ScrollContainer/Panel/AudioStreamPlayer.play()
else:
	$Panel/AudioStreamPlayer.play()
get_tree().change_scene("res://Scene2.tscn")

The sound doesn’t always plays when pressing a button and changing the scene.

:bust_in_silhouette: Reply From: Junton

Write
print(get_tree().current_scene.name)
inside _ready() and call the _ready() in the pressed() signal… and check what does it print: does it only sometimes print the Scene1 or always? If it is always Scene1, then there is something wrong with the sound path. If it isn’t always Scene1, then there is something wrong with the code which switches to that scene.

Like:

func _ready():
>print(get_tree().current_scene_name)

func pressed()
>_ready()
>if get_tree().current_scene.filename == etc......