Input from button attached to other entity

I’m having a hard time with something that’s probably pretty simple in this project. I want to have a counter go up every time the restart button is pressed. However, the restart button is attached to a different entity. I’ve done this because the code also has a setValue function that I guess runs everytime the button screen is reloaded thus also resetting the value, making the counter useless.

I’ve tried many different approaches to get this to work, the latest being the solution from this thread. But the button isn’t found. Any help with the code or an alternative solution to my issue is much appreciated!

Code:
I have 1 script attached to the button with this code:

var BtnClicked = pc.createScript('btnClicked');
// initialize code called once per entity
BtnClicked.prototype.initialize = function() {
};

BtnClicked.prototype.onRelease = function (event) {
   this.app.fire('button:clicked:'+this.entity.name);
};

And I have another script attached to the relevant text element with this code:

var RocketCount = pc.createScript('rocketCount');
// initialize code called once per entity
RocketCount.prototype.initialize = function() {    
    // find our widgets
        this.rocketCount = this.entity.findByName('RocketCount');
    // initialize value to 1
        this.setValue(1);    
};

this.app.on('button:clicked:GameoverRestartBtn', function(){
this.setValue(this.value + 1);
}, this);

// Sets the stat value
RocketCount.prototype.setValue = function (value) {
    // clamp between min and max
    this.value = pc.math.clamp(value, 0, 99);
    // update progress text
    this.rocketCount.element.text = this.value + '+';
};

Looking at the code here, it looks like you’ve put code outside of a function, so you are getting errors about app being null or similar.

Try this for the RocketCount:

var RocketCount = pc.createScript('rocketCount');
// initialize code called once per entity
RocketCount.prototype.initialize = function () {
    // find our widgets
    this.rocketCount = this.entity.findByName('RocketCount');
    // initialize value to 1
    this.setValue(1);

    this.app.on('button:clicked:GameoverRestartBtn', function () {
        this.setValue(this.value + 1);
    }, this);
};

// Sets the stat value
RocketCount.prototype.setValue = function (value) {
    // clamp between min and max
    this.value = pc.math.clamp(value, 0, 99);
    // update progress text
    this.rocketCount.element.text = this.value + '+';
};

You will also need to add a click event listener to your button clicked script. See this tutorial for more information: User Interface - Buttons | Learn PlayCanvas, specifically this section: User Interface - Buttons | Learn PlayCanvas