在协程中添加另一个敌人


using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour
{
public GameObject[] enemies;
private void Start(){
    StartCoroutine("SpawnHandler");
}
private IEnumerator SpawnHandler(){
    float spawnDelay;
    int thisEnemy = 0;
    int nextEnemy = 1;
    GameObject cachedEnemy;
    GameObject cachedEnemyTwo;
    float dieTime;
    while (thisEnemy < 2)
    {
        spawnDelay = Random.Range(3f,6f); //random time, from 1-3
        dieTime = Random.Range(3f,3f);

        yield return new WaitForSeconds(spawnDelay); //wait that time

        cachedEnemy = (GameObject)Instantiate(enemies[thisEnemy],       transform.position, transform.rotation);//spawn enemy, cache him
        StartCoroutine(Kill(dieTime, cachedEnemy));
        //Somewhere here 
    }
}

private IEnumerator Kill(float wait, GameObject enemy){
    yield return new WaitForSeconds(wait);
    Destroy(enemy);
    //or here i need to include the "int nextEnemy". I need it to come after the cached enemy so it needs to have the same random.range value!
    // I need it to have the same value so it spawns exactly 1 second after the cached enemy, like a pair so they spawn from the same
    // random value (very important). So as soon as enemy 1 dieTime runs out, "int nextEnemy" spawns and  runs for 1 second after.
}
}

计划是这样的:int thisEnemy spawn使用spawndelay,它运行3秒(坚持的例子,dieTime)。然后当这个死亡后1秒内产生下一个敌人,这些必须是成对的!然后循环。

谢谢你的帮助,这真是让我心烦意乱!马克斯
StartCoroutine(Kill(dieTime, cachedEnemy, spawnDelay, cachedEnemyTwo, nextEnemy))// <-- pass needed values to Kill
并更改Kill:
private IEnumerator Kill(float wait, GameObject enemy, float spawnDelay, GameObject enemy2, int enemyIndex){
    yield return new WaitForSeconds(wait);
    Destroy(enemy);
    yield return new WaitForSeconds(spawnDelay or 1 second what you need...);
    cachedEnemyTwo = (GameObject)Instantiate(enemies[enemyIndex], transform.position, transform.rotation);
}

我很抱歉,这就是我如何理解你的问题,我很新的IEnumerators希望这有助于。

最新更新