I’m trying to save and load my Player’s progress. I have a serialized class named “gamestate”. This class contains all of the Player’s progress, number of lives, total points, etc. That class also contains a public static gamestate current variable that I use to call later. This class has a save method, which I assigned to a save button. The save method comes from “SaveLoad” class where I have a list of gamestate GameObject. On that method I add a “gamestate.current”. When I try to save or load I get a message that my gamestate is actually NULL. I can’t tell if my variables are not being passed into my gamestate class or my list is being created with null object.
Can anyone help me fix this? Thanks!
Class gamestate
[System.Serializable]
public class gamestate: MonoBehaviour {
public static gamestate current;
public GameObject thirdpersoncontroller;
public GameObject gameover;
public int live=5;
public Text txtlive;
public Text txtscore;
public AudioSource source;
public AudioClip audiogameover;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
txtscore.text = ThirdPersonCharacter.score.ToString();
txtlive.text = live.ToString();
}
void livegameover(){
live= live- 1;
if (live== 0) {
source.clip = audiogameover;
source.Play();
Time.timeScale = 0.0f;
gameover.SetActive(true);
}
}
public void SaveMethod(){
Debug.Log ("save...");
SaveLoad.Save();
}
}
Class SaveLoad
public static class SaveLoad {
public static List <gamestate> savedGames = new List<gamestate>();
public static void Save(){
SaveLoad.savedGames.Add(gamestate.current); //here is gamestate current is NULL.
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create(Application.persistentDataPath + "/savedGames.gd");
bf.Serialize(file,savedGames);
file.Close ();
}