The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

I am making a simple platform game, and trying to add sound effects. I downloaded some as .wav files, and have each attached to an AudioStreamPlayer2D. How would I activate these when I hit a certain object? Here is an example below:

elif object.isingroup("killzn"):

                            *** INSERT SOUND ACTIVATION HERE ***
            # Respawn
            score = 0; 
            #print( + " has died.")
            velocity = Vector2(0, 0); 
in Engine by (12 points)

1 Answer

+1 vote

Just get a reference to your AudioStreamPlayer2D then use the play() function.

onready var audio_player = $AudioStreamPlayer2D # whatever the path to your stream player is


elif object.is_in_group("killzn"):
    # Respawn
    score = 0; 

by (3,906 points)

It seems to work fine, except I get the error message "Attempt to call function 'play' in base 'null instance' on a null instance". Any thoughts?

Either the path to your AudioStreamPlayer2D node is incorrect or you are queue_free()ing it and then calling play() on the player once it has been removed from memory.

Whenever you get an error like that (eg. invalid function call in base null instance or invalid index in base null instance, regardless of the node type, it means that the node that you referenced does not currently exist.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.