微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我在此错误中出错的地方是“不能是迭代器块,因为'void'不是迭代器接口类型”?

如何解决我在此错误中出错的地方是“不能是迭代器块,因为'void'不是迭代器接口类型”?

我遇到以下错误

不能是迭代器块,因为'void'不是迭代器 界面类型

这是我正在使用的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



 public class Pipe001Exit: MonoBehavIoUr
 {
        public AudioSource PipeSound;
        public GameObject FadeScreen;
        public GameObject MainCam;
        public GameObject SecondCam;
        public GameObject PipeEntry;
        public GameObject MainPlayer;
    
        void OnTriggerEnter(Collider col)
        {
            PipeSound.Play();
            FadeScreen.SetActive(true);
            FadeScreen.GetComponent < Animator > ().enabled = true;
            
            yield return new WaitForSeconds(0.495f);
            FadeScreen.GetComponent < Animator > ().enabled = false;
            MainCam.SetActive(true);
            SecondCam.SetActive(false);
            MainPlayer.transform.position = new Vector3(25.5f,1,0.5f);
            PipeEntry.GetComponent < Animator > ().enabled = true;
            FadeScreen.GetComponent < Animator > ().enabled = true;
            
            yield return new WaitForSeconds(0.495f);
            FadeScreen.GetComponent < Animator > ().enabled = false;
            
            yield return new WaitForSeconds(1);
            PipeEntry.GetComponent < Animator > ().enabled = false;
            FadeScreen.SetActive(false);
        }
 }

如何解决此问题?

picture of unity

解决方法

正如Hand Kilian和kosist在评论中告诉您的那样,您不能在yield return ...方法上使用void。您应该在IEnumerable方法上使用。

这是您可以使用代码执行的示例:

public class Pipe001Exit : MonoBehaviour
{
    public AudioSource PipeSound;
    public GameObject FadeScreen;
    public GameObject MainCam;
    public GameObject SecondCam;
    public GameObject PipeEntry;
    public GameObject MainPlayer;

    void OnTriggerEnter(Collider col)
    {
        StartCoroutine(Feedback());
    }

    public IEnumerator Feedback()
    {
        PipeSound.Play();
        FadeScreen.SetActive(true);
        FadeScreen.GetComponent<Animator>().enabled = true;

        yield return new WaitForSeconds(0.495f);
        FadeScreen.GetComponent<Animator>().enabled = false;
        MainCam.SetActive(true);
        SecondCam.SetActive(false);
        MainPlayer.transform.position = new Vector3(25.5f,1,0.5f);
        PipeEntry.GetComponent<Animator>().enabled = true;
        FadeScreen.GetComponent<Animator>().enabled = true;

        yield return new WaitForSeconds(0.495f);
        FadeScreen.GetComponent<Animator>().enabled = false;

        yield return new WaitForSeconds(1);
        PipeEntry.GetComponent<Animator>().enabled = false;
        FadeScreen.SetActive(false);
    }
}
,

这是解决方案:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class Pipe001Exit : MonoBehaviour
{
    public AudioSource PipeSound;
    public GameObject FadeScreen;
    public GameObject MainCam;
    public GameObject SecondCam;
    public GameObject PipeEntry;
    public GameObject MainPlayer;

    void OnTriggerEnter(Collider col)
    {
        PipeSound.Play();
        StartCoroutine(Fade());
    }

    IEnumerator Fade()
    {
        FadeScreen.SetActive(true);
        FadeScreen.GetComponent<Animator>().enabled = true;

        yield return new WaitForSeconds(0.495f);
        FadeScreen.GetComponent<Animator>().enabled = false;
        MainCam.SetActive(true);
        SecondCam.SetActive(false);
        MainPlayer.transform.position = new Vector3(25.5f,0.5f);
        PipeEntry.GetComponent<Animator>().enabled = true;
        FadeScreen.GetComponent<Animator>().enabled = true;

        yield return new WaitForSeconds(0.495f);
        FadeScreen.GetComponent<Animator>().enabled = false;

        yield return new WaitForSeconds(1);
        PipeEntry.GetComponent<Animator>().enabled = false;
        FadeScreen.SetActive(false);
    }
}

Documentation link about Coroutines

,

其他答案和评论已经指出:您不能在yield方法中使用void,而只能在IEnumerator中使用

但是,实际上OnTriggerEnter本身可能就是这样的Coroutine ,我怀疑这是混乱的根源。

您可以轻松做到

IEnumerator OnTriggerEnter (Collider col)
{
    ...
}

,你应该没事。在这种情况下,Unity不会像普通的void方法那样自动调用它,而是将其作为协程启动。


我个人还建议您确保始终仅运行一个例程,并阻塞其他OnTriggerEnter例程,直到当前例程结束:

private bool _alreadyCollided;

IEnumerator OnTriggerEnter (Collider col)
{
    if(_alreadyCollided) yield break;

    _alreadyCollided = true;

    ...

    _alreadyCollided = false;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。