Cards Sprites and scores C#

Hello everyone.

I’m new in Unity and i started making a simple card game. All cards have numbers. Each player ( 2 players ) gets a hand of 6 cards and in each round, both players play a card and the winner of the round is the player who played the card with higher number ( The game is not that boring lol but that’s what you have to know for now ).

I made the card a Prefab that can be draged and droped. But now i’m stuck on how to :

  • Make each card take a random sprite from the “Assets/Resources/Cards” folder when the game launches.
  • Assign to each card the number corresponding and based on the sprite it got from the cards folder, that will be needed later when the score is being calculated to determine the winner of the round.

Please help me writing this script.

Thank you

Good day.

You only need first to create an array woth all the sprites. Make is public so you can easy assign them by dragging in the inspector, or private and use the Resources.LoadAll() function.

public Image CardImages;

Once you have the array, you only need to select 1 random element from the array with:

Image selectedSprite = CardImages[Random.Range(0,CardImages.length)];

So nou you have a random sprite loaded at your selectedSprite variable.