0

I have 4 colors in my List, and I want to randomly display each one of them at a different location. But when I try to do so, more than one color appear at the same time which I don't want. I want all the cards to display and each one of them at a different location.

Below is the code.

using UnityEngine;
using System.Collections;
using System.Collections.Generic; //using generic lists so we can dynamically resize our arrays (lists)
/*
 * This script handles the spawning of the game pieces at the center of the screen
 */

public class Spawner : MonoBehaviour {

    public List<GameObject> pieces = new List<GameObject>(); //array that contains our PREFABBED game pieces to be spawned

    public List<GameObject> areaPieces = new List<GameObject>();    //array of the area piece assets 
    public List<Transform> areaLocations = new List<Transform>(); //array of transforms that is placed around our gamepiece - this is where our areapiece will spawn in

    private Transform areaTransform; //var to randomly select an areaLocation[] 
    private int randomPiece; //dv

    void Start () 
    {
        Spawn (); //call spawn at the start of the game
    }

    //randomly spawns a gamepiece each time this is called
    public void Spawn()
    {
        Instantiate(pieces[Random.Range(0, pieces.Count - 1)], transform.position, transform.rotation); //spawns a game piece randomly
        NewRandomArea();
    }

    //handles random instantiation of the 4 gamepiece areas that spawn around our gamepiece
    public void NewRandomArea() 
    {
        for(int i = 0; i < areaLocations.Count; i++)
        {
            areaTransform = areaLocations[i]; //sets our areaTransform to the first transform in the array - loops through every transform in the array
            areaLocations.RemoveAt(i);
            randomPiece = Random.Range(0, areaPieces.Count); //randomly chooses a number based on the length of our array
            print(areaPieces[randomPiece]); //tells us what cards are printing
            Instantiate(areaPieces[randomPiece], areaTransform.position, areaTransform.rotation); //spawn our areapieces in each 4 location
            //areaPieces.RemoveAt[]; //this doesn't work but if it could it would solve the problem
            //find a way to remove that randomly chosen card piece in the array here so that it doesn't spawn again
        }
    }
}
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
  • it is not clear which is your "List", and I do not see any colors anywhere. Please be *a lot more specific*. – Mike Nakis Dec 12 '15 at 20:05
  • Usually to get a random selection without replacement you randomly sort the list first and then just iterate through the list from start until you have the number of items you want (which could be the whole list). – Dijkgraaf Dec 12 '15 at 20:08

2 Answers2

0

What you need is to shuffle the list and then use the shuffled data sequentially. You need a permutation of the list and shuffling does that.

Find shuffling code on the web or on Stack Overflow.

usr
  • 168,620
  • 35
  • 240
  • 369
0

Try making a second list as a temp and popping each used element from the list.

var colorList = List<Color>(); // color list
var tempList = colorList; // temp list so you don't lose your colors

//make random number between 0 & n

var n = 10;
while( n > 0) {
    var randomNumber = Random.Range(0, n);
    tempList.popAt(randomNumber);
    n--;
}

Or as mention in other comments, the shuffle method is

private static Random rng = new Random();  

public static void Shuffle<T>(this IList<T> list)  
{  
    int n = list.Count;  
    while (n > 1) {  
        n--;  
        int k = rng.Next(n + 1);  
        T value = list[k];  
        list[k] = list[n];  
        list[n] = value;  
    }  
}

Taken from here

Community
  • 1
  • 1
Tomaltach
  • 913
  • 1
  • 11
  • 30