给出意外符号错误和错误的Unity IENumerator“不能成为迭代器块,因为'void'不是迭代器接口类型。”

时间:2019-01-10 00:11:07

标签: c# unity3d ienumerator

我正在尝试在Unity中制作一个跳台游戏,当您死亡时,该游戏应该以文字显示游戏,然后等待3秒钟。但是我得到了这些错误:

error CS1525: Unexpected symbol `(', expecting `,', `;', or `='
error CS1525: Unexpected symbol `IEnumerator'

我的代码是:

using UnityEngine;
using System;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;

public class PlayerController : MonoBehaviour
{

public float speed = 1700.0f;
public GameObject Player;
public Text TheText;

void Start()
{
    TheText.text = "";
}

void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

    GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);

    IEnumerator Wait1()
     { 
        if (Player.transform.position.y < -100.59)
        {
            TheText.text = "You Lost. Try Again!!";
            yield return new WaitForSeconds(5);
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);

        }
    }
    IEnumerator Wait2()
    {
        if (Player.transform.position.y > 210)
        {
            TheText.text = "You Lost. Try Again!!";
            yield return new WaitForSeconds(5);
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
    }

 theres 4 more of those, they're all the same

我希望它在重新启动游戏之前可以等待,但是出现这些错误。 void fixedupdate();语句给出以下错误

cannot be an iterator block because 'void' is not an iterator interface type. 

4 个答案:

答案 0 :(得分:2)

您正在尝试使用在版本7中引入的C#功能,在此代码中称为local functions。不幸的是,Unity仍然使用C#v6,这意味着您需要将这些功能移到FixedUpdate方法之外。例如:

void FixedUpdate()
{
    //snip
}

IEnumerator Wait1()
{
    //snip
}

IEnumerator Wait2()
{
    //snip
}

答案 1 :(得分:1)

您可能必须像这样更改代码:基本上关闭FixedUpdate方法,并将IEnumerator Wait()作为单独的方法。

void FixedUpdate()
{
 float moveHorizontal = Input.GetAxis("Horizontal");
 float moveVertical = Input.GetAxis("Vertical");

 Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

 GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);
}

IEnumerator Wait1()
{ 
    if (Player.transform.position.y < -100.59)
    {
        TheText.text = "You Lost. Try Again!!";
        yield return new WaitForSeconds(5);
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
}
IEnumerator Wait2()
{
    if (Player.transform.position.y > 210)
    {
        TheText.text = "You Lost. Try Again!!";
        yield return new WaitForSeconds(5);
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
}

答案 2 :(得分:1)

IEnumerators应该在类范围而不是函数范围中声明,然后用StartCoroutine()触发

例如

componentDidUpdate

来源:https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

请注意IEnumerator是在类中定义的,而不是在Update()函数中定义的。

在触发协程时也要小心,因为您可以很容易地同时触发多个协程,这在这里并不理想(例如,您可能有3或4个协程试图加载一个关卡所有操作都同时进行,这将导致该关卡被加载3或4次,这会给玩家带来巨大的延迟,并可能带来不必要的副作用)

此外:IEnumerators与Coroutines不同,Coroutines是一种Unity功能,允许代码并行运行(但是没有线程化),并且它们使用IEnumerators(一种C#)功能来工作。

IEnumerators是C#的一项功能,具有超越协程的许多功能,主要用于迭代自定义容器(数组,列表等)

请谨慎使用您的术语,因为它们是2个非常不同(尽管相关)的事物。

答案 3 :(得分:1)

您的代码使用局部函数,即在另一个函数中声明的函数。要使用该功能,您必须升级到Unity 2018.3,并确保Scripting Runtime Version-> Api Compatibility Level中的Player SettingsOther Settings都设置为.NET4.x。这样,您的代码就可以正常工作了。