I have a set of spawn points and an object that I want to spawn from a random spawn point every second. I've used the InvokeRepeating function to make this work. But now, I want to modify the code a little bit. I don't want the the object to be spawned from the same spawn point twice in a row.
Let's say I have 5 spawn points: A, B, C, D, and E. When InvokeRepeating runs, the object is spawned randomly from spawn point C. So when InvokeRepeating runs again, I don't want the object to spawn at spawn point C. It has to randomly pick another spawn point. I don't want the object to spawn at at a particular spawn point twice in a row.
Here my code:
void Start ()
{
GameObject[] SP = GameObject.FindGameObjectsWithTag ("spawnPoint");
spawnPoints = new Transform[SP.Length];
for(int i=0; i < SP.Length; i++)
{
spawnPoints[i] = SP[i].transform;
}
InvokeRepeating("SpawnObject", spawnTime, spawnTime);
}
void SpawnObject()
{
int spawnPointIndex = Random.Range(0,spawnPoints.Length);
Instantiate(theObject, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}
Any idea what I can add to my code so solve this?
↧