Copy random GameObjects in an Array and add to a list. Without having duplicates in the list
hello, I have an array of GameObjects of 50 items. I want to randomly copy a selection of 5 items from the array to a list. Each selection must be unique (no duplicate array items selected). The GameObjects in the Array need to remain unchanged in order to be reused by other gameobjects. (that's why i don't want to use a list in place of the array and just remove items as they get selected.)
I had a thought of having a struct or class which contains the gameobject and an int variable. I could then randomly place a number from 1 to 5 into 5 of the array items and then put these numbered items into the list.
public List<GameObject> myList = new List<GameObject> ();
int value = 0;
public class GameobjectArray
{
public Gameobject myGameObject;
public int Selected;
}
GameObjectArray[] gameobjectarray = new GameObjectArray[50];
void start () { for (int i = 1; i < 6; i++) { int current = random.range(0, 50); if (gamobjectarray[current].Selected != null) gameobjectarray[current].Selected == i; }
for (int value = 1; value < 6; value++) { for (int i = 1; i < 50; i++) { if (gameobjectcarray[i].Selected = value) myList.add (gameobjectarray[i].myGameObject); } } }
Please ignore any typoes. I just quickly threw something together in this editor to give you a bit of an idea of what i am talking about and to ask if there is a better way.
Thanks.