Sprites Multiple - is there an easy way to select a single frame?

Hi,

I am working on some kind of card game. Each card has 3 images on it. (front, back and hidden). So for each card I created a sprite sheet containing all 3 sides. GameObject wise I created a SpriteRenderer and attached a script to it. It contains an array of sprites which will hold the different possible cards and an integer where to set which card we currently have and another one which side of the card is shown.

The code looks like this:

public class CardGame : MonoBehaviour {
public Sprite[ ] allSprites =newSprite[10]; // 10 different cards
public int cardNumber =0; // which card this is
public int shownface =0; // Which side of the card should be shown

// Use this for initialization
void Start () {
SetSprite();
}
void SetSprite()
{
SpriteRenderer sr = GetComponent();
sr.sprite = allSprites[cardNumber];
}
…

So in the editor I attached my 10 different sprites so they fill the Sprite[ ] Array - no problem here.
Then I set the cardnumber and on screen I see the card perfectly all shown with the first image of the single sprite which in this case is the front side.

Now I want to select the second image of the multiple sprite via script without creation an animation or a state so I can handle all cards the same way.

Is there a simple way to select a different part of the texture like:

sr.sprite = sr.sprite[2];

or similar? Or is the only way to change the image to create an animation?

I am a bit lost here.

Any ideas?

Thx.

When you slice the image to form 3 different sprite, you actually getting 3 different Sprite assets there (as sub assets under your original image asset). They should have named such a way “sprite_0” “sprite_1” “sprite_2”. In your example, you have successfully create array of “_0”, which is all the front face. To continue the way you do it, you might need another 2 array that store the back and hidden Sprite assets.

But I am curious. Shouldn’t the back and hidden graphic the same for all the card?

I worked around now by just saving all files in a resource folder and load them in an array of sprites with Resources.LoadAll. Then I get them all frames as single sprites but as you said I needed a second array with all the names. So it’s working now thank you :slight_smile:

The back will be a different picture. It’s not standard playing cards but something with a picture on one side and a word on the other side. For hidden I am considering color codes for groups but probably that will be the same on every card.

Oh, interesting. Looks like something unique is cooking :wink:

Glad that it’s working. Good luck in your game making!

1 Like