Spawn in a different and random Spawn point from a list
Hello,
I'm building my first game and I need some help. I want to spawn a prefab in a Spawn point from a list. And make sure before spawned that there isn't already an active coin there (tried to destroy one if there was but they both get destroyed at the same time since its the same object). Imagine something like the old "Snake Game" on Nokia but in 3D with 5-10 active coins at once, spawned on specific Spawn Point from an array, and never 2 on the same point. Here is my basic code:
public GameObject Coin; // The Coin prefab to be spawned.
public float spawnTime = 15f; // How long between each spawn.
public Transform[] spawnPoints; // An array of the spawn points this Coin can spawn from.
void Start()
{
InvokeRepeating("SpawnCoin", 0, spawnTime);
}
void SpawnCoin()
{
// Find a random spawn point.
int spawnPointsIndex = Random.Range(0 , spawnPoints.Length);
// Create an instance of the Coin prefab at the randomly selected spawn point's position and rotation.
Instantiate(Coin, spawnPoints[spawnPointsIndex].position, spawnPoints[spawnPointsIndex].rotation);
}
I tried different approaches like using if or while loops with increments along with changing the range of the spawn points but it always crashes or gets completely out of sync spawning 10-20 coins a second. Also tried to use only Instantiate in if statements but nothing seems to work right. Does anyone have any ideas?
Thanks a lot in advance ;)