I have this codes but when I run, sometimes the gameobjects spawn at the same time and at the same place. I need help.
public Transform player;
public float maxTime;
public float minTime;
public int valuespawn = 2;
public GameObject[] spawnPrefabs;
public Transform[] spawnLocation;
int currentPrefabs;
int currentLocation;
void Start () {
spawner ();
}
void Update(){
if (player.transform.position.x > 40)
CancelInvoke ();
}
void spawner () {
List spawnPoint = new List (spawnLocation);
for (int i = 0; i < valuespawn; i++) {
if (spawnPoint.Count <= 0)
return;
currentPrefabs = Random.Range (0, spawnPrefabs.Length);
currentLocation = Random.Range(0, spawnPoint.Count);
Transform pos = spawnPoint [currentLocation];
spawnPoint.RemoveAt (currentLocation);
Instantiate (spawnPrefabs [currentPrefabs], pos.position, pos.rotation);
Invoke ("spawner", Random.Range (minTime, maxTime));
}
}
↧