希望有人可以帮我解决这个问题。我试图将IEnumerator与我的对象池脚本结合,以实例化wave中的敌人。但是我不能让它们实例化而不会立即摧毁自己2到3次然后再实例化一波敌人并且再也不会产生。任何人都可以帮我看看我在哪里出错吗?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SpawnMaster : MonoBehaviour {
public int enemyCount = 4;
public float spawnWait=0.5f;
public float startWait=1;
public float waveWait=3;
public Transform[] spawnPoints;
private int currentWave = 0;
private int waveCount = 10;
private IEnumerator spawnWaves;
private IEnumerator goHorde;
bool isSpawning = false;
void Update()
{
if(!isSpawning)
{
isSpawning = true;
StartCoroutine(SpawnWaves(currentWave));
}
}
IEnumerator SpawnWaves (int currentWave)
{
Debug.Log("Spawning " + currentWave + "currentWave");
for (int i = 0; i < waveCount; i++)
{
StartCoroutine(GoHorde());
}
currentWave += 1;
yield return new WaitForSeconds (waveWait);
//We've spawned, so start another spawn
isSpawning = false;
}
IEnumerator GoHorde()
{
GameObject beetle01 = BeetlePool.current.GetPooledObject();
if(beetle01 == null)
yield return null;
// Only pick a new spawn point once per wave
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
for (int j = 0; j < enemyCount; j++)
{
beetle01.transform.position = spawnPoints[spawnPointIndex].position;
beetle01.transform.rotation = spawnPoints[spawnPointIndex].rotation;
beetle01.SetActive(true);
yield return new WaitForSeconds (spawnWait);
}
}
}
答案 0 :(得分:0)
我会改变:
如果(!isSpawning)
为:
if(isSpawning!= true)
或者:
if(isSpawning == false)
此外,如果您要进行实例化和销毁的多波敌人。您应该查看对象池以节省内存。