Trying to set an Image sprite on runtime


#1

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!


#2

Try this…

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


#3

It works, thank you!