How to enable a certain entity when a 3d button is pressed?

can someone pls provide me with a demo project that works

To enable an entity when a 3D button is pressed in PlayCanvas, you can use the “onclick” event of the button to trigger a function that sets the “enabled” property of the entity to true.

Here’s an example code snippet that shows how to do this:

// get a reference to the 3D button entity
var buttonEntity ='ButtonEntity');

// get a reference to the entity you want to enable
var targetEntity ='TargetEntity');

// add an onclick event to the button entity
buttonEntity.element.on('click', function () {
    // set the enabled property of the target entity to true
    targetEntity.enabled = true;

In this code, we first use the findByName method to get references to both the 3D button entity and the entity we want to enable. Then, we add an onclick event to the button entity using the on method. When the button is clicked, the function inside the on method is executed and the enabled property of the target entity is set to true, which will enable it. Note that you’ll need to customize the findByName method calls to match the names of the entities in your project. Also, make sure to add this code to a script that is attached to the entity containing the button.

can you give a demo project pls
thank you

@NOT_pars Hi and Welcome. I altered one of the Playcanvas tutorials so you can see how this is done. Here is the link to the project.

Here is the code that makes it work. Show/Hide button enables/disables entity based on press.

var ButtonLogic = pc.createScript('buttonLogic');
ButtonLogic.attributes.add('description', {type: 'string'});

// initialize code called once per entity
ButtonLogic.prototype.initialize = function() {
    this.entity.button.on('click', function(event) {
        this.Item ='Spaceship');
        if(this.description == 'Show') this.Item.enabled = true;
        if(this.description == 'Hide') this.Item.enabled = false;

    }, this);

thank you @Tirk182

1 Like