gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:47:31 27 4
gpt4 key购买 nike

我正在尝试在我的游戏中使用 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 stackOverFlow 不允许我在没有最后几行的情况下发帖。)

最佳答案

需要注意的重要一点是,协程仅在其附加的游戏对象保持事件状态时运行。来自Coroutine文档:

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

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

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com