Unity c#无法从迭代器返回值。使用yield return语句返回一个值,或者使用yield break来结束迭代

时间:2017-05-04 09:38:04

标签: c# unity3d compiler-errors pc

你好我得到了这个错误,我更新了团结 Assets / TCG / Scripts / card.cs(1232,71):错误CS1622:无法从迭代器返回值。使用yield return语句返回一个值,或者使用yield break来结束迭代

我看了很多次使用它的代码,我无法修复它我寻找修复但是我不能理解那么好所以有人可以帮助我继承代码如果你需要孔代码告诉我我将添加它,这是错误显示的部分。

IEnumerator PayAdditionalCostAndPlay()
{
    if (DiscardCost > 0 && ValidSpell())
    {
        Player.ActionCancelled = false;
        Player.targets.Clear();
        Debug.Log("this card has an additional discard cost");

        for (int i = 0; i < DiscardCost; i++)
        {
            Player.NeedTarget = 21; // a card in hand to discard

            while (Player.NeedTarget > 0)
            {
                yield return new WaitForSeconds(0.1f);
            }
            if (Player.ActionCancelled) 
            { 
                Debug.Log("action cancelled"); 
                return false; 
            }
        }

        foreach (GameObject target in Player.targets) //discard
        {
            target.GetComponent<card>().Discard();
        }
    }
}

3 个答案:

答案 0 :(得分:2)

你不能在协同例程中使用return,如果你想停止协同例程执行使用yield break而不是return false,如下所示 -

IEnumerator PayAdditionalCostAndPlay()
{       
    if (DiscardCost > 0 && ValidSpell()) 
    {
        Player.ActionCancelled = false;
        Player.targets.Clear();
        Debug.Log("this card has an additional discard cost");
        for (int i = 0; i < DiscardCost; i++) 
        {
            Player.NeedTarget = 21; // a card in hand to discard

            while (Player.NeedTarget > 0)
                yield return new WaitForSeconds (0.1f);
            if (Player.ActionCancelled) {
                Debug.Log("action cancelled");
                yield break;
            }
        }
        foreach (GameObject target in Player.targets) //discard
            target.GetComponent<card>().Discard();
    }
}

答案 1 :(得分:0)

单一方法不能yield returnreturn。你必须选择其中一个。

答案 2 :(得分:0)

我怀疑当ActionCancelled设置为true时,你只是试图退出协同例程,稍作修改就会结束。

您无法在合作例程中返回值,但您可以做的是调用外部方法并BREAK(停止其中的forloop轨道)

    IEnumerator PayAdditionalCostAndPlay()
    {
        if (DiscardCost > 0 && ValidSpell())
        {
            Player.ActionCancelled = false;
            Player.targets.Clear();
            Debug.Log("this card has an additional discard cost");
            for (int i = 0; i < DiscardCost; i++)
            {
                Player.NeedTarget = 21; // a card in hand to discard

                while (Player.NeedTarget > 0) yield return new WaitForSeconds(0.1f);
                if (Player.ActionCancelled)
                {
                    Debug.Log("action cancelled");
                    OnPlayerActionCancelled(); // optional, obviously
                    break; // stop processing the for-loop
                }
            }
            if (!Player.ActionCancelled)
            {
                foreach (GameObject target in Player.targets) //discard
                    target.GetComponent<card>().Discard();
            }
        }
    }

    private void OnPlayerActionCancelled()
    {
        // Do something when a player action is cancelled
    }