我试图在游戏中使用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;
    }
}


我很想在这里获得一些帮助,因为这对我的游戏至关重要,而且我无法在线找到答案,因此,如果有人决定帮助解决该问题,我将不胜感激。 (糟糕的stackOverFlow让我在没有最后一行的情况下发帖。)

最佳答案

需要注意的重要一点是,协程仅在其附属的游戏对象保持活动状态时运行。从Coroutine文档中:


  当GameObject时,协程也停止
   使用SetActive(false)禁用了它所附加的对象


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

关于c# - 为什么脚本在第一个WaitForSeconds()之后忽略该部分?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59329212/

10-12 19:39