[SOLVED] Uncaught TypeError: Cannot read property 'sound' of undefined

Hello!
I’m getting this error when i want to pause music.
I’ve created an entity called UserInterface which contains two components:
First component is my sound and the second is my script.
My scene is : PlayCanvas | HTML5 Game Engine
When i click the secod button i want to pause the music but i get this error.
Could somebody tell me what’s the problem?
Thanks!

Change musicButton to:

User.prototype.musicButton = function(event) {
    
    // get button element by class
    var button2 = this.div.querySelector('.button2');
    
    // add event listener on `click`
    button2.addEventListener('click', function() {
            
          if (playing) {
             this.entity.sound.pause('music');
             playing = false;
          }
          else if (!playing){
             this.entity.sound.play('music');
             playing = true;
          }
            
         }.bind(this));
};

Read all about bind here.

1 Like

Thank you very much!