i am trying to create a simple texas hold'em poker game in unity, I have 52 sprites (images) of each card in a deck
i am trying to load player cards in the beginning of a round, (2 cards for each player) for n number of players all respective to 52 cards in the deck.
so what i am trying to do, is first generate a random number and then based on that number, i load the specific sprite but my unity freezes and nothing works and i am aware that this approach will not work if i replicate my Card Prefab which is bound with Get_Card Class, So i need another Approach. HELP thanks
Random Number Class
public class Random_Number
{
const int n = 52;
int[] all_num = new int[n];
int number = 0;
void Start()
{
for (int i = 0; i < n; i++)
{
all_num[i] = i;
}
}
// Update is called once per frame
void Update()
{
}
public int get_me_a_number()
{
while (number == 0)
{
int rand = Random.Range(0, n);
number = all_num[rand];
all_num[rand] = 0;
}
Debug.Log(number.ToString());
return number;
}
}
Card Class
public class Get_Card : MonoBehaviour
{
public Sprite[] Sprite_pic;
int number;
Random_Number number_class = new Random_Number();
// Start is called before the first frame update
void Start()
{
number = number_class.get_me_a_number();
GetComponent<SpriteRenderer>().sprite = Sprite_pic[number];
}
ISSUE is now fixed, Freezing issue was caused by an infinite loop.
numberinRandom_Numberis a class member so you will return the same card everytime because you are not resetting it back to zero. (Probably not related to freezing) - Oguz Ozgul