为什么脚本会忽略第一个 WaitForSeconds() 之后的部分?

Why does the script ignore the part after the first WaitForSeconds()?

我正在尝试在我的游戏中使用 WaitForSeconds() 来执行一个场景。 我很乐意在这里改进和解决不工作的问题 WaitForSeconds()(它只是在我开始使用 WaitForSeconds() 之后忽略了这部分) 剧本:

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 GameObject mainPanel;

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

    public GameObject foodPanel;

    public GameObject bird;

    private int _lastFood;
    private int _hunger;

    void Update()
    {
        _hunger = PlayerPrefs.GetInt("_hunger");
        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)
    {
        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();
                _lastFood = j;
                return;
            }
            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];
                _lastFood = j;
                return;
            }
            if (isFull[j] == true && int.Parse(itemAmounts[j].GetComponent<Text>().text) == 0)
            {
                isFull[j] = false;
                return;
            }
        }
    }

    public void foodButtonsBehavior(int a)
    {
        if(a >= 0 && a <= 5)
        {
            StartCoroutine(foodEat(slots[a]));
        }
        if (a == 7) //add food button
        {
            addItem(7, "Special Seeds", itemImages[1], 2);
        }
    }
    public void closeFoodMenu()
    {
        foodPanel.SetActive(false);
    }

    public IEnumerator foodEat(GameObject slot)
    {
        mainPanel.SetActive(false);
        foodPanel.SetActive(false); // Start Of Animation
        itemAmounts[_lastFood].GetComponent<Text>().text = (int.Parse(itemAmounts[_lastFood].GetComponent<Text>().text) - 1).ToString();
        moveFood(-1f, -1f); // Resetting Position for the food
        foodObject.GetComponent<SpriteRenderer>().sprite = foodImages[_lastFood];
        yield return new WaitForSeconds(1.1f);
        print("Continuing");
        foodObject.SetActive(true);
        //bird.transform.Rotate(0, 0, 0);
        bird.transform.position = new Vector2(0, -2.4f);
        yield return new WaitForSeconds(0.7f);
        moveFood(-1f, -2f);
        bird.transform.Rotate(0, 0, 0);
        bird.transform.position = new Vector2(0, -2.4f);
        yield return new WaitForSeconds(0.7f);
        moveFood(-1f, -2.7f);
        bird.transform.Rotate(0, 0, 0);
        bird.transform.position = new Vector2(0, -2.4f);
        yield return new WaitForSeconds(0.4f);
        foodObject.GetComponent<SpriteRenderer>().sprite = foodHalfImages[_lastFood];
        bird.transform.Rotate(0, 0, 0);
        bird.transform.position = new Vector2(0, -2.4f);
        yield return new WaitForSeconds(0.4f);
        foodObject.SetActive(false);
        bird.transform.Rotate(0, 0, 0);
        bird.transform.position = new Vector2(0, -2.4f);
        yield return new WaitForSeconds(0.8f);
        PlayerPrefs.SetInt("_hunger", _hunger + 24);
        foodPanel.SetActive(true); // End Of Animation
        mainPanel.SetActive(true);
    }
    private void moveFood(float x, float y)
    {
        foodObject.transform.position = new Vector2(x, y);
    }
    public void changeBird(GameObject x)
    {
        bird = x;
    }
}

我很想在这里得到一些帮助,因为这对我的游戏很重要,而且我无法在网上找到答案,所以如果有人决定帮助解决这个问题,我将不胜感激。 (Srry Whosebug 不会让我 post 没有最后几行。)

需要注意的重要一点是,协程仅 运行 当它所附加的游戏对象保持活动状态时。来自 Coroutine 文档:

A coroutines also stops when the GameObject it is attached to is disabled with SetActive(false)

此行为与您看到的错误一致,尽管有点令人困惑,因为代码似乎在您禁用该对象后继续 运行ning。有一个非常细微的解释,但对于一个简化版本:SetActive(false) 等到帧结束以停止游戏对象上的 MonoBehaviours 运行ning。