Changing images on button pressed

What I want to do is I have 2 image slot which will show playing cards cover on default , when player pressed a button, both slots will randomly changed into a new card.

I know some stupid way of doing this …that I add all playing cards in advance to these two slots(which will create dozens of canvas), and then I check the variable and enable the one canvas representing be card.but I seriously doubt there will be better way.

So…what’s the best way to do so?thanks guys.

To change the image on button is really easy
create variable that stores the image or a list. when you select what card to play or what image you want to play then use a code like this. sorry if the capitals are not correct.
this.gameObject.GetComponent().sprite = WhatEverTheNewImageIs;

thanks, I will try this out :slight_smile:

Or you can try these. Take an array of sprites and then select any random sprite from that array

public Sprite[] allSprites;

public void ButtonClicked()
{
    //these is for UI
    getComponent<Image>().sprite = allSprites[Random.range(0, allSprites.length);

   //if your working on gameObject then use getComponent<SpriteRenderer>().sprite = allSprites[Random.range(0, allSprites.length);
}
1 Like

this is definitely what I want, thx