Switch between set of images after pressing key

Hi guys!

On my UI I have an image which has to be updated as soon as I press a certain key (e.g. the heart image is changed to a coin image). In this case I just want to try to call the method by using


I don’t want to press a button on the UI. Now I want to switch between to images but I don’t know how I can reference between the two images. I saw solutions like these:

currentImage.sprite = newImage;

My sprites are not really images but I don’t want to create images out of them. Do I have to do it or can I do it with the sprites? And what other solutions are there?

Kind regards

I think you are just misunderstanding the UI image component.
In a UI canvas, you have to use special UI components instead of normal ones. In this case if you want a sprite in the UI you need to use UI image instead of Sprite renderer. you can then change which sprite is displayed by setting the sprite of the UI image with
image.sprite = newSprite;
In this case “image” is the UI image component in your UI, not an image asset you want to display, and newSprite is the asset to display.