Am I doing this "matching game" in a correct way?

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

Hey @fadhelz,

Good job on getting the code to where you have, something we think to be a simple task is usually one that is not.

I’ve created some code to help you out, with the color changes.

Some sudo code;

  • Loop though our game objects,
  • If the count of the object is divisable by 2 (since we’re working with 8 objects int total)
  • Grab the current Object and then the next object.
  • Assign those objects a single random color;
  • Change the objects material colors to the random color.
using System.Collections.Generic;
using UnityEngine;

public class ChoiceAssignment : MonoBehaviour
{
    public List<GameObject> itemsToAssignTo = new();
    public List<Color> definedColors = new();
    
    // Start is called before the first frame update
    void Start()
    {
        var colorMather = new ColorMather();
        for (int i = 0; i < itemsToAssignTo.Count; i++)
        {
            //Get a random color.
            var rndColorIndex = Random.Range(0, definedColors.Count);
            
            // Divide our gameObjects count by 2, then assign our pairs
            if (i % 2 != 0 || (i >= itemsToAssignTo.Count - 1)) continue;
            
            // Create our pairs, assign the pairs a color, remove color from the list.
            colorMather.CreatePairs(itemsToAssignTo[i],itemsToAssignTo[i + 1], definedColors[rndColorIndex]);
            definedColors.RemoveAt(rndColorIndex);
        }
    }

    private class ColorMather
    {
        private GameObject _objectPairOne;
        private GameObject _objectPairTwo;

        public void CreatePairs(GameObject pairA, GameObject pairB, Color color)
        {
            _objectPairOne = pairA;
            _objectPairTwo = pairB;
            ChangeObjectsColor(color);
        }

        private void ChangeObjectsColor (Color color)
        {
            _objectPairOne.GetComponent<MeshRenderer>().material.color = color;
            _objectPairTwo.GetComponent<MeshRenderer>().material.color = color;
        }
    }
}

image

I haven’t give you everything that you’ve asked for but this would be a good starting point to further learn and improve on the code. I can’t make it too easy :wink:

Hope this helps!