event click box to play music

Hello friends, I’m trying to learn about clicking on the box to play the piece of music, I can’t find the document, can you help me. Thank you

Learn mouse events:

  1. Basic Mouse Input | Learn PlayCanvas
  2. Mouse | PlayCanvas API Reference

Then add a Audio Component:

  1. Basic Audio | Learn PlayCanvas

After attaching the Audio component to button add a sound file to it. When you need to play it just use the following:
this.entity.sound.play("[ Name you defined in the Audio Component ]");
When the button is clicked.

1 Like

please let me know what i’m doing is that every time i press the box, there will be a sound. But mine is having an error that i can only click once out sound, hope someone can help answer the question. Thanks
here is my project link PlayCanvas 3D HTML5 Game Engine

Hi @Phong_Nguy_n_Van ,

The boxStates script that you use requires to fill up some attributes for it to work. Parse the script first by clicking on the button here and they will appear in editor:


Check the buttons tutorial on how to use them:


i have edited and still showing this error. You click on the box and you will see that it only plays music 1 click i want it to play music many times after each click. Thank you for your help

You need to add some images to your project much like the tutorial I posted above does. The error message basically says “Hey I need those files to function, you haven’t provided them yet”.


I added a photo and it still gives me this error

That’s a good start, now fill the second one as well.

By the way did you look at the tutorial I’ve sent? It contains an updated script that’s much easier to use and leverages the button component.

Here is the example project: https://playcanvas.com/editor/scene/547900

the example you gave me i have seen and understood a bit but i still haven’t solved the problem i think the error is here

if you don’t mind can you edit my example to make it easier for me to understand. Thank you very much

Hi @Phong_Nguy_n_Van! I think the problem right now is that your entity doesn’t have an element component on it while your script tries to use that component.

If you want to play a sound when you clicking on a 3D box, maybe it’s better to start with the project of the tutorial below. Instead of activate the pulse effect you can play your sound.


1 Like