I'm working on the save system for a local co-op game I'm working on. The goal of the code is to establish a Serializable Static class that has instances of the four players and the relevant data they need to store for saving in binary.
[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;
}
}
The Player
Class just contains the ints that track player stats and a bool for if they are in the alive state or not. My issue comes when one of my classes in the game scene needs to get data from this static class. When the static class is referenced, it throws the error.
void Start () {
mageAlive = GameState.current.mage.isAlive;
if (mageAlive == true)
{
mageMainHealth = GameState.current.mage.mainHealth;
mageSecondHealth = GameState.current.mage.secondHealth;
} else
{
Destroy(this);
}
}
I am new to coding so I'm not sure how Unity interacts with static classes that don't inherit from MonoBehaviour
. I based this code off of a tutorial that worked pretty similarly, so I'm not sure what the issue is.