[SOLVED] Sound Problems with Buttons

So in my project I have 3 different parents in the scene called ‘All’. Each parent has their corresponding sound entity. When keyboard is enabled the five buttons on the right work. Which means when they are clicked the sound that was playing before stops and the corresponding sound plays. However using the same buttons, same sound and same script this does not work when the drums and guitar parent is enabled.

Editor - https://playcanvas.com/editor/scene/902608
Code Editor - https://playcanvas.com/editor/code/677188?tabs=29748685

Please let me know if you do not understand the problem.
Thank you.

1 Like

Hi @Krish_USCan,

I think the issue is in your click handlers:

this.app.root.findByName('RockSound') ... // etc

When the script runs it will search for the first entity named RockSound. That will always be the RockSound entity under keyboard_Parent parent entity.

FindByName() returns the first entity found in the hierarchy by that name.

To fix that you will have to create variations of that script, that search under the right parent.

this.app.root.findByName('drums_Parent').findByName('RockSound') ... // etc
1 Like

I would also consider using entity references script attributes instead and reference them in the editor.


@yaustar we would have considered this, but @Krish_USCan has recently started playcanvas scripting, and I am busy with the other parts of the app. For sure, it’s an elegant solution, but I think @Leonidas’s idea is a bit easier to grasp for @Krish_USCan.