How to add script to entity programatically?

I have

this.data.forEach((pos, i) => {
            const entity = new pc.Entity(`trigger${++i}`);
            entity.addComponent("collision");
            entity.collision.halfExtents = new pc.Vec3(3, 0.5, 3);
            entity.addComponent("script");
...
});

but how can I add to entity existing trigger.js script in code?
this is alternative to selecting script in editor
question about not engine only

1 Like

Hi @grzesiekmq,

Take a look at the create method on the script component, there is an example for that in the docs:

https://developer.playcanvas.com/en/api/pc.ScriptComponent.html#create

2 Likes