[SOLVED] How to enable and disable an entity in code

0 voters

in those two it looks 2d and all. i need to know how to enable and disable the whole entity so the collisions wont work when it is disabled. pls help

Set the property enabled on the Entity to true/false

https://developer.playcanvas.com/en/api/pc.Entity.html#enabled

i mean in code

That is how you do it in code. eg:

this.entity.enabled = false;
1 Like

ok, that was short

wait how do you do that with a button press i want to make it to where the button disables all but one of the skins i want and enables one skin

Hi Kevin,

You can get the reference of the button and listen to the “click” event of the button and enable/disable skins. You can do something like this:

this.entity.button.on('click', this.onButtonClick, this);

You can refer to this document on using buttons
https://developer.playcanvas.com/en/tutorials/ui-elements-buttons/

1 Like

ok it registers my button click but there doesnt seem to be any disabling in this code or enabling

You can enable an entity like this:

this.entity.enabled = true

Or disable it like this:

this.entity.enabled = false

how do i direct the disablin/ecnabling so i can enable/disable certain etities

What do you mean by direct the enabling/disabling?

You can find the entities in the hierarchy by using
let entity = this.app.root.findByName("someEntity");
Or use script attributes
MyScript.attributes.add('someEntity', { title: 'Entity', type: 'entity' });
to get a reference to that entity and enable or disable it.

1 Like

i dont understand any of :frowning:

what if it is not this entity

What is it instead?