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
}
}
}