How do I spawn game objects where only 2 or a certain amount of game object has the same material?

Hi, I am making a card game that needs to be limited to two sets of decks of cards. How do I limit the set to n decks of cards? Currently in the code sample, im able to spawn a amount of gameobjects based on the amount of material in the list * 2 . Any help would be appreciated.

public class CardControl : MonoBehaviour
{
public List<Material> materialList;
public GameObject prefab;
private Vector3 pos;
private Quaternion rot = Quaternion.identity;
private bool Same;

private float XPosition = 0f;
private float YPosition = 0f;
private float ZPosition = 0f;

private void Awake()
{
    pos = new Vector3(XPosition, YPosition, ZPosition);
    Same = false;


    for (int i = 0; i < (materialList.Count * 2); i++)
    {
        Material mat = RandomMaterial(materialList);
        
        InstantiateWithMaterial(prefab, pos, rot, mat);
        
    }
}

public Material RandomMaterial(List<Material>_List_)
{

    return _List_[Random.Range(0, _List_.Count)];
    
}

public void InstantiateWithMaterial(GameObject _prefab_, Vector3 _pos_, Quaternion _rot_, Material _mat_)
{
    GameObject obj_ = Instantiate(_prefab_, _pos_, _rot_);
    obj_.gameObject.GetComponent<MeshRenderer>().material = _mat_;
    obj_.name = _mat_.name + "1";
}
}

So first I would model a deck of cards you can do it a bunch of ways but the easiest way would probably just be using a list of integers i.e. 1-13 or maybe an Enum.

If you want to ascribe some metadata you could create a card struct or class like:

public class Card
{
    public int Value { get; private set; }
    
    ...
}

Now I would create a card factory class or method whose job is to just create a full suite and then a full deck of cards which returns them as a flat list or Array of cards i.e. public List<Card> GetNewDeck();


Then I would create a new List<Card> property in your game somewhere and call AddRange(GetNewDeck()) twice, either in a loop or manually depending on how configurable it needs to be.


Now you have your data/ game state in order then you can simply say that when an item is of type “spades” then render x texture using basic control flow like an if or a switch statement.

i.e.

switch(card.Suit) {
    case Suit.Diamonds:
        Material.SetTexture(...);
        break;
    ....
}

Code Example