我正在尝试在更改场景(生命和宝石)时保留一些变量。
使用DontDestroyOnLoad
,对象不会破坏,但变量会破坏,并重置为5个生命和0个宝石。
这是我的代码
GameControlle
void Start ()
{
GameObject keeper = GameObject.Find ("VARKEEPER");
varkeeper = keeper.GetComponent<KeepVars> ();
}
void Update ()
{
LiveChecker();
}
void LiveChecker()
{
if (lives == 5) {
lives1.enabled = true;
lives2.enabled = true;
lives3.enabled = true;
lives4.enabled = true;
lives5.enabled = true;
} else if (lives == 4) {
lives1.enabled = true;
lives2.enabled = true;
lives3.enabled = true;
lives4.enabled = true;
lives5.enabled = false;
} else if (lives == 3) {
lives1.enabled = true;
lives2.enabled = true;
lives3.enabled = true;
lives4.enabled = false;
lives5.enabled = false;
} else if (lives == 2) {
lives1.enabled = true;
lives2.enabled = true;
lives3.enabled = false;
lives4.enabled = false;
lives5.enabled = false;
} else if (lives == 1) {
lives1.enabled = true;
lives2.enabled = false;
lives3.enabled = false;
lives4.enabled = false;
lives5.enabled = false;
} else if (lives <= 0) {
lives1.enabled = false;
lives2.enabled = false;
lives3.enabled = false;
lives4.enabled = false;
lives5.enabled = false;
}
}
VarKeeper
public int lives;
public int gems;
GameControlle gameController;
// Use this for initialization
void Awake ()
{
DontDestroyOnLoad (this);
}
void Start ()
{
GameObject player = GameObject.Find ("Character");
gameController = player.GetComponent<GameControlle> ();
}
// Update is called once per frame
void Update () {
lives = gameController.lives;
gems = gameController.gems;
}
我用来获取变量的系统可能不合适..
答案 0 :(得分:0)
好的,只是解决了它。
基本上我所做的是让VarKeeper控制所有变量(宝石和生命)。我在GameController中更改了包含lives
到varkeeper.lives
的所有内容,以及与宝石相同的内容。
基本上解决了它