问题描述
我遇到以下错误:
不能是迭代器块,因为'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);
}
}
如何解决此问题?
解决方法
正如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;
}