Trying to set an Image sprite on runtime

Hi, i’m trying to set an sprite of an Image at runtime, but the image just erases the old sprite and becomes white.
Heres a sample project: https://playcanvas.com/editor/scene/753917

I’m referencing the Image and the Sprite asset here:

SpriteSetter.attributes.add(“anonSprite”, {type: “asset”, assetType: “sprite”});
SpriteSetter.attributes.add(‘avatarImage’, {
type: ‘entity’
});

Then i’m just setting the sprite like this:

this.avatarImage.element.sprite = this.anonSprite.sprite;

Thanks!

Try this…

this.avatarImage.element.sprite = this.anonSprite.resource; // change sprite to resource

2 Likes

It works, thank you!

1 Like