I'm having some trouble spawning a random asteroid from an array within a for loop.
The array is working now, however (I assume this is in the for loop somewhere) it wont alternate between the different game objects. whichever object spawns first, spawns every time, but each time you load the game it spawns a different one every time.
How do I get it to chose a new random object from the array after every spawn instance?
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour {
public GameObject[] asteroids;
public Vector3 spawnValues;
public int asteroidCount;
public float spawnWait;
public float startWait;
public float waveWait;
void Start () {
StartCoroutine (spawnWaves ());
}
IEnumerator spawnWaves () {
GameObject asteroid = asteroids[Random.Range(0, asteroids.Length)];
while (asteroidCount > 0) {
for (int i = 0; i < asteroidCount; i++) {
Vector3 spawnPosition = new Vector3 (spawnValues.x, Random.Range (-spawnValues.y, spawnValues.y), spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (asteroid, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire