Hi, I am a new learner so hopefully, you’ll be patient with me, I am taking a course right now but at the same time I like to challenge myself by doing something much different than what I take from the course.
After I got familiar with UI canvases and elements, I decided to do this small memory match game, I am actually stuck on the first step of the steps I planned to do, in this first step I decided to randomize four colors over 8 boxes at Start, where each color should be assigned to only 2 boxes of the 8, I used lists and for loop to achieve this, while it works fine for a number of times some times I get 3-4 boxes with the same color, and I never found why that happens, this is my method code
void AssignColors()
{
List<GameObject> choiceBoxClone = new List<GameObject>(choiceBox);
List<Color> colorOptionsClone = new List<Color>(colorOptions);
List<int> repeatCountClone = new List<int>(repeatCount);
for(int i=0; !isAllColorShown; i++)
{
randomNum = Random.Range(0, colorOptionsClone.Count);
loopCounter++;
if(i >= choiceBoxClone.Count)
{
i = 0;
}
if(repeatCountClone[randomNum] >= 2)
{
colorOptionsClone.Remove(colorOptionsClone[randomNum]);
repeatCountClone.Remove(repeatCountClone[randomNum]);
if(colorOptionsClone.Count == 0){isAllColorShown = true;}
}
else
{
repeatCountClone[randomNum]++;
Debug.Log("random number is =" +randomNum);
choiceBoxClone[i].GetComponent<Image>().color = colorOptionsClone[randomNum];
choiceBoxClone[i].GetComponentInChildren<TextMeshProUGUI>().text = ""+repeatCountClone[randomNum];
choiceBoxClone.Remove(choiceBoxClone[i]);
}
}
}
this is a screen shot for the game when it misses and assign the color for three boxes
as I am new here I am not sure that I provided enough information for you to help, so please ask me if you need any more details.
also, is what I am doing considered a good scripting practice? is there a better way? if I want to learn the proper practices in game development about topics like for example, “Is spamming if statements the best way to go” or “Is developing in a minimum number of scripts good or not” where do I find a guide that specializes in good developmental habits and practices
and one final thing, for this specific game, what is a better way to go for it?
thanks