【发布时间】:2016-02-05 14:49:08
【问题描述】:
我正在为我正在开发的本地合作游戏开发保存系统。代码的目标是建立一个可序列化的静态类,该类具有四个玩家的实例以及他们需要存储的相关数据以保存在二进制文件中。
[System.Serializable]
public class GameState {
//Current is the GameState referenced during play
public static GameState current;
public Player mage;
public Player crusader;
public Player gunner;
public Player cleric;
public int checkPointState;
//Global versions of each player character that contains main health, second health, and alive/dead
//Also contains the checkpoint that was last activated
public GameState()
{
mage = new Player();
crusader = new Player();
gunner = new Player();
cleric = new Player();
checkPointState = 0;
}
}
Player 类只包含跟踪玩家统计数据的整数和一个布尔值,用于判断他们是否处于活动状态。当我在游戏场景中的一个类需要从这个静态类中获取数据时,我的问题就出现了。当引用静态类时,它会抛出错误。
void Start () {
mageAlive = GameState.current.mage.isAlive;
if (mageAlive == true)
{
mageMainHealth = GameState.current.mage.mainHealth;
mageSecondHealth = GameState.current.mage.secondHealth;
} else
{
Destroy(this);
}
}
我是编码新手,所以我不确定 Unity 如何与不继承自 MonoBehaviour 的静态类交互。我的这段代码基于一个工作原理非常相似的教程,所以我不确定问题是什么。
【问题讨论】:
标签: c# unity3d static nullreferenceexception serializable