【问题标题】:Unity c# Cannot return a value from iterators. Use the yield return statement to return a value, or yield break to end the iterationUnity c# 无法从迭代器返回值。使用 yield return 语句返回一个值,或者使用 yield break 结束迭代
【发布时间】:2017-05-04 09:38:04
【问题描述】:

你好,我收到了这个错误,我更新了统一 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();
        }
    }
}

【问题讨论】:

    标签: c# unity3d compiler-errors


    【解决方案1】:

    你不能在协程中使用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();
        }
    }
    

    【讨论】:

      【解决方案2】:

      单个方法不能同时使用yield return 和return。您必须选择其中之一。

      【讨论】:

        【解决方案3】:

        我的怀疑是,当 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
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-11-17
          • 2023-03-14
          • 2012-12-07
          • 2022-01-15
          • 2012-08-07
          • 2012-04-09
          • 1970-01-01
          相关资源
          最近更新 更多