gpt4 book ai didi

c# - 在 Unity 中将 bool 值从一个场景保存到另一个场景

转载 作者:行者123 更新时间:2023-12-05 06:17:04 25 4
gpt4 key购买 nike

我正在尝试制作一款冒险游戏,该游戏会保存不同的 bool 值以便进入下一个场景。问题是一个 bool 在另一个场景中使它为空。有什么建议吗?

public class Farmer : MonoBehaviour
{
public bool NextLevel = false;
public Cow1 cow1;
public Cow2 cow2;
public Cow3 cow3;
public Dialogue dialogue;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
if (GetComponent<Cow1>().getIsDisplayed() && GetComponent<Cow2>().getIsDisplayed() && GetComponent<Cow3>().getIsDisplayed())
{
NextLevel = true;
}
if (NextLevel == true)
{
FindObjectOfType<DialogueManager>().startDialogue(dialogue);
}
}
}

最佳答案

是的,有一种非常简单的方法可以做到这一点,只需为游戏对象提供 DontDestroyOnLoad 属性

{
public bool NextLevel = false;
public Cow1 cow1;
public Cow2 cow2;
public Cow3 cow3;
public Dialogue dialogue;
// Start is called before the first frame update
void Start()
{
DontDestroyOnLoad(this.gameObject);
}

// Update is called once per frame
void Update()
{
if (GetComponent<Cow1>().getIsDisplayed() && GetComponent<Cow2>().getIsDisplayed() && GetComponent<Cow3>().getIsDisplayed())
{
NextLevel = true;
}
if (NextLevel == true)
{
FindObjectOfType<DialogueManager>().startDialogue(dialogue);
}
}
}

这将使您的游戏对象在场景变化时保持不变

关于c# - 在 Unity 中将 bool 值从一个场景保存到另一个场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61802659/

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