Spawn 9 players with 3 colors (red, yellow, green) and a minimum and maximum of 3 players with each color

Good people!
It’s my first post and I’ve been going crazy for like two days.
I have managed without problems to be able to spawn the players with different colors by means of the code

materials [Random.Range (0, materials.Length)]

So far so good because each player assigns them to me a material and they can be repeated but my idea is up to a maximum of 3 players with each color (as if they were 3 teams).
I am blocked and I need your help.
I have tried various for, while, if, etc with no positive results.

    GameObject playerInstantiate;
    public GameObject[] spawnPoints;
    public Material[] materials;

spawnPoints = GameObject.FindGameObjectsWithTag ("Spawn");
            for (int i = 0; i <spawnPoints.Length; i ++)
                int colorNum = Random.Range (0, 3);

                playerInstantiate = Instantiate (player, spawnPoints  _.transform.position, spawnPoints  *.transform.rotation);*_

playerInstantiate.transform.SetParent (spawnPoints .transform);

playerInstantiate.GetComponent () .material = materials [Random.Range (0, materials.Length)];
The problem comes that I don’t know how to solve in the loop if there are already 3 players with the same color and pass or assign the next color.
I hope it has been understood and I think the code I want to do is not very difficult but I am blocked and I do not want to do it manually in case in the future I can expand the number of players.
By the way I have found similar themes but I have not been able to adapt them to the expected result. If the post already exists please apologize.
Regards, and thank you very much

I think maybe you should use something like the factory pattern (even if its just a factory method) to generate your players. That way you can just ask it to generate as many as you want with a specific configuration i.e. give me 3 red, yellow and blue.

As far as your other question. Why would you need to generate them more than once per game? You shouldn’t be destroying them. Just use object pooling methodologies to put them back in a pool for later use. Then you will always only have the players you spawned at the start fo the game and wouldn’t incur any cost if you needed to reuse them during the game.

thanks for your answer. I will investigate the factory pattern as I did not know it.

thanks for your answer. I will investigate the factory pattern as I did not know it.