[SOLVED] Script Help when 1 script is applied to multiple buttons

So I have a script that I applied to two UI buttons. I’d like to have specific statements within the initialize section for each button. But I can’t seem properly identify which button the script instance is applied to. Or maybe I have the wrong idea about what a script instance is.

The console message is created no matter which UI button is clicked, well of the two that the script has been added to anyway. I did add the script ‘attribute’ and gave each UI button a unique name, all to avoid the ‘findbyname’ method. But when I click a button with a different attribute name, that console.log event should not happen.

var HighlightComponent = pc.createScript('highlightComponent');

HighlightComponent.attributes.add('BtnName', {
    type: 'string'
});

// initialize code called once per entity
HighlightComponent.prototype.initialize = function() {

    if (this.entity.btnName = "btnRose") {
        this.entity.element.on('click', function(event) {
            // change the model from one type of Rose to the other
            console.log("Clicked Button...".concat(this.entity.btnName));
        }, this);
    };

};

Got this to work by using the = properly (i.e. ===). Javascript is new to me, but I am struggling through. lol

var HighlightComponent = pc.createScript('highlightComponent');

// initialize code called once per entity
HighlightComponent.prototype.initialize = function() {

    if (this.entity.name === "btnRose") {
        this.entity.element.on('click', function(event) {
            // change the model from one type of Rose to the other
            console.log("Clicked btnRose Button...");
        }, this);
    };
};


// update code called every frame
HighlightComponent.prototype.update = function(dt) {

};
1 Like