Hi.
I wrote this code and i want this code don’t repeat number for make coin. this code only work correctly when CurrentCoinNumber <= 3 but when it’s great than 3, i wrote another code for reset it to 1 and compared RandCoin with LastCoin value in While loop for to avoid duplicate value but it make my code don’t work correctly. Does anyone know where the problem is?
This is my code:
int CurrentCoinNumber = 1;
int LastCoin;
List<int> RandCoinList = new List<int> { 10, 10, 10 };
void Awake()
{
for (int j = 0; j < 8; j++)
{
// Create Coin
int RandCoin = Random.Range(0, 3);
if (CurrentCoinNumber <= 3)
{
while (RandCoinList.Contains(RandCoin))
{
RandCoin = Random.Range(0, 3);
}
LastCoin = RandCoinList[RandCoin];
}
else
{
// reset all RandCoinList index value to 10
for (int k = 0; k < RandCoinList.Count; k++)
{
RandCoinList[k] = 10;
}
while (RandCoin == LastCoin)
{
RandCoin = Random.Range(0, 3);
}
CurrentCoinNumber = 1;
}
RandCoinList[RandCoin] = RandCoin;
GameObject coinObj = Instantiate(Coin[RandCoin], GameObject.Find("Coins").transform);
CurrentCoinNumber += 1;
}
}