- Home /
Question by
Sam-Robichaud · Mar 23, 2012 at 02:01 AM ·
array
Selecting 2 items from array without selecting the same item twice
I'm trying to select 2 items from an array, and want to make sure i don't pick the same array item twice, but i'm not sure how to go about doing that.
Below is a sample of my script...
var aColors = new Array (); //array for the list of colors below
aColors[0] = Color.red;
aColors[1] = Color.green;
aColors[2] = Color.blue;
aColors[3] = Color(1, 0.3, 0); //Orange
aColors[4] = Color.yellow;
aColors[5] = Color(0.4, 0.0, 1); //Purple
randomColor = Random.Range(0,5);
finalColor = (colors[randomColor]);
secondColor = //any array item except finalcolor
Comment
Best Answer
Answer by DaveA · Mar 23, 2012 at 02:03 AM
secondRandom = Random.Range(0,5);
while (secondRandom == randomColor)
secondRandom = Random.Range(0,5);
secondFinalColor = colors[secondRandom];
There are many ways, this is one. Another is where you generate an array of random values, then sort it, also sorting the indices, then the indices are in random order, but you know there's only one of each, so you can just pick the first two. And so on.
Thanks i had to look up while loops but I think i get it now, thanks for your help.
Your answer
