Access button in script

I cannot find documentation to access button through script. I am trying to use active = false to set the button to deactive state. How do I achieve this?

var ButtonScript= pc.createScript('buttonScript');

ButtonScript.attributes.add('myButton', { type: 'entity' });

so basically myButton.active = false. I do not want the entity to be deactive, instead button attached on entity should be deactive.

How do I do this?

Hi @bernieDev and welcome! Based on your description it should be something like below.

this.myButton.button.active = false;
1 Like

Hi thanks and if there is an array of buttons how do i do then?

I’m not sure how you mean that, but I guess you need to loop through the array and disable it on every entity.

Loop through an array of stated entity attributes.

var MyScript = pc.createScript('myScript');

MyScript.attributes.add('ENTITY_ARRAY', {
    type: 'entity',
    array: true,
    default: ['', '']
});

MyScript.prototype.update = function (dt) {
    for (var i = 0; i < this.ENTITY_ARRAY.length; i++) {
        this.ENTITY_ARRAY[i].button.active = false;
    }
};

In my code I only stated two entity slots but you can easily add more by adding more ’ ’ to the default attributes code section for ENTITY_ARRAY.

Please note that if you re-parse after changing your default list slots amount you are going to need to re-state your entities in the Editor. So make sure that you have a rough idea of the right amount of button entities that you are going to be using.

Test Demo

https://playcanvas.com/project/865086/overview/demo-update-script-attributes

1 Like

I wonder why you need to do this instead of just a number for the length of the array. :face_with_monocle:

Maybe for for allowing individual defaults for separate slots? Not sure.

I personally prefer to use the use input when disabling a button I would use something like this to go through an array.
for (let i = 0; i < myarray.length; i++) {

    let entity = myarray[i];

   entity.element.useinput=false;

}