简体   繁体   English

为什么这个 IEnumerator function 不起作用

[英]Why does this IEnumerator function not work

In this code I try to make a system in which before a prefab i made spawns, a particle effect appears to warn the player that the prefab will spawn there, but the prefab and particle effect don't even spawn;在这段代码中,我尝试创建一个系统,在我制作的预制件产生之前,粒子效果似乎警告玩家预制件将在那里产生,但预制件和粒子效果甚至不产生; please help.请帮忙。 I am new to coding so try to phrase the answers in the simplest way possible.我是编码新手,所以尽量以最简单的方式表达答案。

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);
                
            }
        }

    }
}

I think you mean something like:我认为你的意思是:

    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.

相关问题 为什么 using 语句在 IEnumerator 上起作用,它有什么作用? - Why does the using statement work on IEnumerator and what does it do? 以下为什么不工作? (IEnumerable / IEnumerator) - Why doesn't the following work? (IEnumerable/ IEnumerator) GC如何使用IEnumerator和yield? - How does GC work with IEnumerator and yield? 为什么我的 IEnumerator<AppiumWebElement> 没有 .Where()? - Why does my IEnumerator<AppiumWebElement> not have .Where()? 为什么 IEnumerator<T> 继承自 IDisposable 而非泛型 IEnumerator 不? - Why does IEnumerator<T> inherit from IDisposable while the non-generic IEnumerator does not? 为什么接口 IEnumerable 返回 IEnumerator GetEnumemrator()? 为什么不只实现接口 IEnumerator? - Why does interface IEnumerable return IEnumerator GetEnumemrator()? Why not just implement interface IEnumerator? 为什么我必须定义IEnumerator <T> .Current&IEnumerator.Current,它实现了什么? - Why must I define IEnumerator<T>.Current & IEnumerator.Current, and what does that achieve? 在按钮中对 Onclick function 使用 IEnumerator,但场景管理器不起作用 - Using IEnumerator for Onclick function in a Button, but scenemanager doesnt work 为什么统一给我关于 IEnumerator 的错误 - Why does unity gives me error about IEnumerator 带有语音命令的 IEnumerator function - The IEnumerator with voice command function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM