簡體   English   中英

為什么這個 IEnumerator function 不起作用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM