Why is the sound not playing

Hello, I am trying to make a button that when clicked plays a sound and then switches scene. So far it switches scenes and doesn’t play the sound.

(C#) (godot-4)

This is how I have the code setup. I most certainly am doing it wrong.

Thanks

Do you have an audio file in the stream player? Another issue might be that your audio is in the scene you have just left, so it plays but the scene is changed before you hear anything.

1 Like

I’ve since checked both and tested both of suggestions but no dice. However, I did not notice it was giving me an error in the debugger. It says “void playbutton._Pressed(): System.NullReferenceException: Object reference not set to an instance of an object.” Im new to coding so I dont really know how to fix this.

I don’t code in C#, so I am not sure exactly. It seems like it’s telling you that there is nothing there.

  1. Show us where you are declaring the music variable, perhaps that is where the issue lies.
  2. Take a screenshot of the inspector with the AudioStreamPlayer selected so we can see the settings.
  3. Try making a constant of the audio file you are using and assign the stream with code right before you play it. (I don’t know how to do this in C#, so you’re on your own with figuring out the syntax)

Why are you using C# and not GDScript?

Where it is declared:


I did change up the code somewhat. but still i didnt get any effect.

Inspector with AudioStreamPlayer:


image

I will try to do the third thing you mentioned.

I am using C# so that I can practice a language that has more use cases outside of game dev.

I might be wrong, but It looks like you’re playing the sound and then changing the scene that has the sound in it, so the sound plays but the scene changes and so the sound goes bye bye with the scene.

What I am doing with my game is I have a master scene that adds the gameplay scene as a child, then whenever I need to switch gameplay scenes I delete the old scene and add the new one as a child. So I am never really changing scenes, just the child nodes, meaning I can have an AudioStreamPlayer permanently in the scene.

1 Like

this is why i dont like the changescenetofile method, it just clears everything in the scene tree root node and switched to new scene.
so like @TwinTailDigital said, the sound is indeed started playing but it just got destroyed (the audiostreamplayer) the moment it switched to that new level1 scene, hence no sound played
you might want to look at autoload for AudioManager or really dont use changescenetofile method to switch scene to be viewed.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.