【问题标题】:The IEnumenator is making problems [duplicate]IEnumenator 出现问题 [重复]
【发布时间】:2020-04-07 04:23:49
【问题描述】:

我试图统一制作一个 c# 脚本,而脚本在第 61 行(在脚本末尾的行首和行尾标有 **)中提到了 IEnumerator。 这里有什么问题,我该如何解决?

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


public class FoodManager : MonoBehaviour
{

    public bool[] isFull;
    public GameObject[] slots;

    public GameObject[] itemNames;
    public GameObject[] itemImages;
    public GameObject[] itemAmounts;
    public GameObject foodObject;

    public string[] foodNames;
    public Sprite[] foodImages;
    public Sprite[] foodHalfImages;

    public GameObject foodPanel;
    void Update()
    {
        for(int i = 0; i < 6; i++)
        {
            if (isFull[i] == true)
                slots[i].SetActive(true);
            if (isFull[i] == false)
                slots[i].SetActive(false);
        }
    }

    private void addItem(int max, string itemName, GameObject itemImage, int addingAmount, string foodName)
    {
        for (int j = 0; j < max; j++)
        {
            if (isFull[j] == true && itemNames[j].GetComponent<Text>().text == itemName)
                itemAmounts[j].GetComponent<Text>().text = (int.Parse(itemAmounts[j].GetComponent<Text>().text) + addingAmount).ToString();
            if (isFull[j] == false)
            {
                isFull[j] = true;
                itemNames[j].GetComponent<Text>().text = itemName;
                itemAmounts[j].GetComponent<Text>().text = addingAmount.ToString();
                itemImages[j].GetComponent<Image>().sprite = foodImages[j];
                break;
            }
        }
    }

    public void foodButtonsBehavior(int a)
    {
        if(a >= 0 && a < 6)
        {
            StartCoroutine(foodEat(slots[a]));
        }
        if(a == 6) //exit Button
            foodPanel.SetActive(false);
    }

    **public IEnumerator foodEat(GameObject slot)**
    {
        foodPanel.SetActive(false);
    }
}

我想使用 IEnumenator,因为我想将 WaitForSeconds() 添加到脚本中,但我还没有添加。 请帮我解决这个问题,这很重要!

【问题讨论】:

  • 您有一个声明为public IEnumerator foodEat(GameObject slot) 的函数,但它没有return 语句(更不用说返回IEnumerator 的语句了)。如果您希望它返回一个集合,请返回一个。如果您不希望它返回任何内容,请将函数声明为 void 函数
  • 我在 Google 上搜索了 "not all code paths return a value" 并找到了 this 和大量其他答案。

标签: c# unity3d ienumerator


【解决方案1】:

在 IEnumerator 函数中,您应该至少有一个 yield return smth,对于您的情况,它是 yield return new WaitForSeconds(secondsToWait)

【讨论】:

    【解决方案2】:

    问题是该方法的返回类型为IEnumerator,但没有返回任何内容。在你完成 foodEat 方法之前它不会编译,因此它会返回一个 IEnumerator

    【讨论】:

    • 非常感谢
    【解决方案3】:

    您没有包含退货声明。您的函数签名提到它将返回一个 ienumerator,但在您的 1 行代码中,您只设置了一个值,没有返回任何内容。要么写一个 return 语句,要么使用 void 关键字!

    【讨论】:

      【解决方案4】:

      如您所见https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html,设置活动不会返回任何内容。所以把你的代码改成

      public void foodEat(GameObject slot)
      {
          foodPanel.SetActive(false);
      }
      

      这应该可以解决您的问题。

      【讨论】:

        猜你喜欢
        • 2015-08-03
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 2016-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-24
        相关资源
        最近更新 更多