[SOLVED] Enabling an entity with button script

I’m trying to enable and disable an entity using a button.

I’ve attached a script to the button which includes the declared a script attribute as the target entity (contentPanel) I want to enable and disable.

Currently it launched with the contentPanel being disabled. But if I click the button with the script, it does not work.

“Uncaught TypeError: Cannot read properties of undefined (reading ‘enabled’).”

var ButtonEntityActive = pc.createScript('buttonEntityActive');

ButtonEntityActive.attributes.add('contentPanel', {type: 'entity', description: 'The entity that needs to be activated/deactivated'});

// initialize code called once per entity

ButtonEntityActive.prototype.initialize = function() {

    this.contentPanel.enabled = false;

    this.entity.button.on('click', function(event) {

            this.contentPanel.enabled = true;

    });
};

declares this before

that=this; 
this.contentPanel.enabled = false;

    this.entity.button.on('click', function(event) {

            that.contentPanel.enabled = true;

    });
1 Like

You need to pass the this scope to the event register

    this.entity.button.on('click', function(event) {
            this.contentPanel.enabled = true;
    }, this); // See `this` variable being passed as a param
1 Like

Thank you this sorted the problem.