C# 使用Unity可在多个场景中保持玩家健康

C# 使用Unity可在多个场景中保持玩家健康,c#,unity3d,C#,Unity3d,我现在有一个2d平台,玩家一开始就有9条生命,每次遇到障碍都会失去一条生命 GameManager脚本存储并显示健康状况,如果玩家失去生命,实体脚本将在GameManager中更新健康状况 如果玩家失去生命,他们可以在该关卡重生 完成一个关卡后,玩家进入一个新的场景“关卡完成”,在那里他们可以选择继续下一关卡或退出游戏 但是,当播放机继续到下一个级别时,currentHealth变量不会持续并重置为0 我知道为什么会出现这个问题,但我不知道如何解决它。 在LevelComplete中的任何位置都

我现在有一个2d平台,玩家一开始就有9条生命,每次遇到障碍都会失去一条生命

GameManager脚本存储并显示健康状况,如果玩家失去生命,实体脚本将在GameManager中更新健康状况

如果玩家失去生命,他们可以在该关卡重生

完成一个关卡后,玩家进入一个新的场景“关卡完成”,在那里他们可以选择继续下一关卡或退出游戏

但是,当播放机继续到下一个级别时,currentHealth变量不会持续并重置为0

我知道为什么会出现这个问题,但我不知道如何解决它。 在LevelComplete中的任何位置都不会调用currentHealth,因此在GameManager中启动时,它已丢失currentHealth的值

我不能在LevelComplete上调用GameManager,因为它将生成玩家。所以我不确定如何传递变量。 这是我的脚本

游戏经理
你能不能仅仅声明一个静态变量来跟踪?我很确定这会让你在你的场景中保持持久性,并且对你的其他人有用scripts@StevenSmith这种方法往往导致难以发现的bug。单身汉使测试变得困难,他们违反了SRP,如果你不密切关注,他们可能会产生奇怪的副作用。真正的单身汉是测试的麻烦,但仍然有用,我只能想出两种替代方法,1可能在第一个场景中设置一个隐藏的游戏对象,然后对其应用脚本,并确保在加载新场景时不会破坏它(我觉得这与静态声明的方法几乎相同),或者可能对一个简单的文本文件进行外部持久化作为其数据,这将为其他变量的存储打开一些大门,唯一的问题是增加了复杂性,不确定OP是否愿意这样做,你会推荐David什么?是否允许?哦,等等,看看这个,玩家优先,忘记这个,可能会对你有帮助。我使用了一个静态变量,当我移除“Level Complete”场景时,它似乎工作得很好。
using UnityEngine.UI;
using System.Collections;

public class GameManager : MonoBehaviour {


public GameObject player;
    private GameCamera cam;
    private GameObject currentPlayer;
    private Vector3 checkpoint;

    public Text livesText;
    private Entity livesCount;
    public static int startHealth = 9;
    public int currentHealth;
    public bool playerDeath = false;

    public static int levelCount = 2;
    public static int currentLevel = 1;

    void Start () {
        cam = GetComponent<GameCamera> ();
        livesText.text = currentHealth.ToString ();

        if(GameObject.FindGameObjectWithTag("Spawn")){
            checkpoint = GameObject.FindGameObjectWithTag("Spawn").transform.position;
        }

        SpawnPlayer (checkpoint);
    }

    private void SpawnPlayer(Vector3 spawnPos) {
        currentPlayer = Instantiate (player, spawnPos, Quaternion.identity) as GameObject;
        cam.setTarget(currentPlayer.transform);
    }

    public void Update(){
        if(!currentPlayer){
            if (Input.GetButtonDown ("Respawn")) {
                playerDeath = false;
                SpawnPlayer (checkpoint);
            }
        }
        if (playerDeath) {
            Destroy (currentPlayer);
        }
        if (currentPlayer) {
            currentHealth = currentPlayer.GetComponent<Entity> ().GetHealth ();
        }
        livesText.text = currentHealth.ToString ();
    }

    public void SetCheckPoint(Vector3 cp){
        checkpoint = cp;
    }

    public void EndLevel(){
        if(currentLevel < levelCount){
            currentLevel++;
            Application.LoadLevel("Level Complete");
        }
        else{
            Application.LoadLevel("Game Complete");
        }
    }
}
using UnityEngine;
using System.Collections;

public class Entity : MonoBehaviour {
    public static int currHealth;
    private int checkHealth; //Make sure 0 < health < 9

    public void ModifyHealth(int amount){
        checkHealth = currHealth + amount;

        if (checkHealth >= 9) {
            currHealth = 9; //Health can't be greater than 9
        }
        else if(checkHealth <= 0){
            Die (); //End game if health is less than or equal to 0
        }
        else{
            currHealth = checkHealth; //Otherwise, update Health
        }
    }

    public int GetHealth(){
        return currHealth;
    }

    public void SetHealth(int currentHealth){
        currHealth = currentHealth;
    }

    public void Die(){
        Application.LoadLevel ("Game Over");
    }
}
using UnityEngine;
using System.Collections;

public class LevelComplete : MonoBehaviour {
    public GUISkin gSkin;

    void OnGUI(){
        GUI.skin = gSkin;

        if(GUI.Button(new Rect((Screen.width/2) - Screen.width *0.15f,Screen.height/2 - Screen.height *0.05f,Screen.width*0.3f,Screen.height*0.1f),"Next Level")){
            Application.LoadLevel ("Level 2");
        }
        if(GUI.Button(new Rect((Screen.width/2) - Screen.width *0.15f,Screen.height/2 + 20.0f,Screen.width*0.3f,Screen.height*0.1f),"Quit")){
            Application.Quit ();
        }
    }
}