How can I add animation dynamically (with code)?

Hello,

I would like to add animation asset to animation component dynamically, but I couldn’t way to add new animations to it.

https://developer.playcanvas.com/en/api/pc.AnimationComponent.html

I tried to modify assets array and trigger some events but, that didn’t work. Is there any way to do that?

Best regards

Okay, I just figured out how to do it.

As far as I understand it doesn’t catch the push event, but directly setting it works.

var animations = entity.animation.assets;
animations.push(animationId);

entity.animation.assets = animations;

1 Like

Hi @commention,

Yes, nice fine, thanks for sharing your solution.

You need to set a reference to the changed array, not push directly to it. That’s how the property getter/setter will act on the change.

1 Like