IENumerator 每次调用时都不会产生,并且随机范围不会在每次运行时重置

我正在创建一个流星雨,它从游戏对象列表中选择一个随机的空游戏对象。我希望每 2.33f 实例化一颗流星。然后程序应从列表中选择一个新的随机生成区域并重复直到 smallWaveCounter 达到 0。此代码有效但它会不停地实例化流星,直到 smallWaveCounter 达到 0 并且实例化之间没有暂停。


我的问题是:为什么 IENumerator 似乎只在第一次运行时产生?如何让它在每次触发时更改随机对象?


[SerializeField]

private List<GameObject> meteorSpawnSmall = new List<GameObject>();

 private int smallCounter;


 private void Start()

{

    foreach(GameObject spawn in meteorSpawnSmall)//Counts objects in list

    {

        smallCounter++;

    }

 }


void Update()

{


 if (showerStart == true)

    {            

            StartCoroutine(SmallShower());


        if(smallWaveCount <= 0)

        {

            showerStart = false;

        }

    }

  }


IEnumerator SmallShower()

{

    if (smallWaveCount > 0)

    {

        yield return new WaitForSeconds(smallWaveTimer);// smallWaveTimer = 2.33f

        smallPickerNum1 = Random.Range(0, smallCounter - 1); // Needs to randomize each run

        smallPickerNum2 = Random.Range(0, smallCounter - 1);

        while (smallPickerNum2 == smallPickerNum1)

        {

            smallPickerNum2 = Random.Range(0, smallCounter - 1);

        }

        getSpawnZone1 = meteorSpawnSmall[smallPickerNum1];//Grabs the spawnzone from list

        getSpawnZone2 = meteorSpawnSmall[smallPickerNum2];//Grabs the 2nd spawn zone from list

        Instantiate(smallMeteor, getSpawnZone1.transform.position, Quaternion.identity);

        Instantiate(smallMeteor, getSpawnZone2.transform.position, Quaternion.identity);

        smallWaveCount--;//smallWaveCount starts at int 10

    }

}


qq_遁去的一_1
浏览 95回答 1
1回答

RISEBY

它确实每次都在等待,但你每帧都执行协程,所以你无法区分差异。改变你的代码有点像:void Update(){&nbsp;if (showerStart == true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; showerStart = false; //to ensure that your coroutine called once&nbsp; &nbsp; &nbsp; &nbsp; StartCoroutine(SmallShower());&nbsp; &nbsp; }}IEnumerator SmallShower(){&nbsp; &nbsp; while (smallWaveCount > 0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; yield return new WaitForSeconds(smallWaveTimer);// smallWaveTimer = 2.33f&nbsp; &nbsp; &nbsp; &nbsp; smallPickerNum1 = Random.Range(0, smallCounter - 1); // Needs to randomize each run&nbsp; &nbsp; &nbsp; &nbsp; smallPickerNum2 = Random.Range(0, smallCounter - 1);&nbsp; &nbsp; &nbsp; &nbsp; while (smallPickerNum2 == smallPickerNum1)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; smallPickerNum2 = Random.Range(0, smallCounter - 1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; getSpawnZone1 = meteorSpawnSmall[smallPickerNum1];//Grabs the spawnzone from list&nbsp; &nbsp; &nbsp; &nbsp; getSpawnZone2 = meteorSpawnSmall[smallPickerNum2];//Grabs the 2nd spawn zone from list&nbsp; &nbsp; &nbsp; &nbsp; Instantiate(smallMeteor, getSpawnZone1.transform.position, Quaternion.identity);&nbsp; &nbsp; &nbsp; &nbsp; Instantiate(smallMeteor, getSpawnZone2.transform.position, Quaternion.identity);&nbsp; &nbsp; &nbsp; &nbsp; smallWaveCount--;//smallWaveCount starts at int 10&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP