[SOLVED] Create pc.CollisionComponent with code

I can’t figure out how to create the pc.CollisionComponent with code.

This is my falty attempt, it caches when i attempt to set the type to box, witch let me to belive that I had no clue :smiley:

 var obj = this.app.root.findByName("Box1")
 var cc = new pc.CollisionComponent(this.app, obj);
cc.type = 'box';

You don’t create components like that. Instead you use entity.addComponent( componentName, componentData );

In your case you can do:

obj.addComponent('collision', {type: 'box'});

Thank you, that solved it for me, guess that is pretty fundamental knowledge:)

Here’s a related tutorial https://developer.playcanvas.com/en/tutorials/programmatically-creating/

1 Like