我在for循环中从数组中生成一个随机小行星时遇到了一些麻烦。
数组现在正在工作,但是(我假设这是在某个地方的for循环中)它不会在不同的游戏对象之间交替。无论哪个对象首先生成,每次都会生成,但每次加载游戏时,每次都会产生不同的对象。
如何在每个生成实例之后从数组中选择一个新的随机对象?
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);
}
}
}
}
答案 0 :(得分:2)
选择要在循环外生成的对象
GameObject asteroid = asteroids[Random.Range(0, asteroids.Length)];
需要在这里
while (asteroidCount > 0) {
for (int i = 0; i < asteroidCount; i++) {
GameObject asteroid = asteroids[Random.Range(0, asteroids.Length)];