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
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.
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.
Why are you using C# and not GDScript?
Where it is declared:
Inspector with AudioStreamPlayer:
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.
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.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.