0

希望有人可以帮助我解决这个问题。我正在尝试将 IEnumerator 与我的对象池脚本结合起来,以实例化波中的敌人。但是我不能让它们实例化而不立即摧毁自己 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);
  }      
 }
}
4

1 回答 1

0

我会改变:

if(!isSpawning)

至:

如果(是产卵!=真)

或者:

如果(isSpawning == 假)

此外,如果您要实例化和摧毁多波敌人。您应该研究对象池以节省内存。

于 2016-12-27T06:05:39.320 回答