![](/img/trans.png)
[英]Why does the using statement work on IEnumerator and what does it do?
[英]Why does this IEnumerator function not work
在这段代码中,我尝试创建一个系统,在我制作的预制件产生之前,粒子效果似乎警告玩家预制件将在那里产生,但预制件和粒子效果甚至不产生; 请帮忙。 我是编码新手,所以尽量以最简单的方式表达答案。
using System.Collections;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using UnityEngine;
using System.Linq;
using System.Diagnostics;
public class EnemySpawner : MonoBehaviour
{
public GameObject enemy;
float randX;
Vector2 whereToSpawn;
public float SpawnRate = 2f;
float nextSpawn = 0.0f;
public GameObject marker;
// Start is called before the first frame update
void Start()
{
StartCoroutine(Thing());
}
// Update is called once per frame
IEnumerator Thing()
{
if(true)
{
if (Time.time > nextSpawn)
{
nextSpawn = Time.time + SpawnRate;
randX = Random.Range(-8.79f, 8.79f);
whereToSpawn = new Vector2(randX, transform.position.y);
Instantiate(marker, whereToSpawn, Quaternion.identity);
yield return new WaitForSeconds(1);
Destroy(marker);
Instantiate(enemy, whereToSpawn, Quaternion.identity);
}
}
}
}
我认为你的意思是:
while(true)
{
yield return new WaitForTime(SpawnRate-1);
randX = Random.Range(-8.79f, 8.79f);
whereToSpawn = new Vector2(randX, transform.position.y);
Instantiate(marker, whereToSpawn, Quaternion.identity);
yield return new WaitForSeconds(1);
Destroy(marker);
Instantiate(enemy, whereToSpawn, Quaternion.identity);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.