Enable/disable entitys with JS button

How would you enable an entity and disable the previous one with the click of a button?

Hi @Jacob_Mcbride,

To get started with buttons, check this tutorial:

https://developer.playcanvas.com/en/tutorials/ui-elements-buttons/

To enable/disable an entity all you have to toggle is this property:

// disable entity
entity.enabled = false;

How would I apply the enable/disable to a non-text-based entity with the following code?

var ButtonLogic = pc.createScript('buttonLogic');
ButtonLogic.attributes.add('textEntity', {
    type: 'entity',
    description: 'The entity that we want to update when the button is clicked'
});
ButtonLogic.attributes.add('description', {type: 'string'});

// initialize code called once per entity
ButtonLogic.prototype.initialize = function() {
    this.entity.button.on('click', function(event) {
        this.textEntity.element.text = this.description;
    }, this);
};

I imagine like this:

    this.entity.button.on('click', function(event) {
        this.textEntity.enabled = !this.textEntity.enabled;
    }, this);
1 Like

But the entity is NOT a text entity, and I need another previous entity disabled (for selecting skins)

Users on the forum cannot share the exact code you need, because they don’t know what you want and how your hierarchy looks like. You only get examples that you can modify to make it work for your needs. You need to study the code to understand which part you need to change to make it work for you.

In the code above textEntity can be every entity. The writer of the code calls it textEntity because it make sense in their situation. You can call it skinEntity for example.

2 Likes

You’re right. I need to study harder.