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
Input.GetKeyDown(KeyCode.DownArrow);
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
CodePudding user response:
I am not sure what you mean by your sprites are not images. But you should be able to assign a sprite to your UI image element.
Here is a sample
public Sprite my_sprite;
public Image img;
void Update()
{
if(Input.GetKeyDown(KeyCode.DownArrow))
{
img.sprite=my_sprite;
}
}
CodePudding user response:
Try this
public Sprite heartSprite;
public Sprite coinSprite;
public Image img;
void Update()
{
bool isPressed = Input.GetKeyDown(KeyCode.DownArrow);
img.sprite = isPressed ? coinSprite : heartSprite;
}