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!

Now the code :

Class gamestate:


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;

  void Awake(){
    current = this;

  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;
      Time.timeScale = 0.0f;  

  public void SaveMethod(){
    Debug.Log ("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");
  file.Close ();
  • \$\begingroup\$ Where do you assign a value to gamestate.current? Also, in the past I have not been able to serialize MonoBehaviour classes using .NET serialization — not sure whether this has changed? \$\endgroup\$ – DMGregory Jul 4 '18 at 1:04
  • \$\begingroup\$ It would be useful to post where you assign to gamestate.current, what calls the Save function, etc. At the moment the issue looks like you are using some global gamestate object and then assuming the save function has access to it, which is bad practice in general. \$\endgroup\$ – blurry Jul 4 '18 at 2:41
  • \$\begingroup\$ I add the awake method to initialize the current one, I'm getting the variables in the other class but it gives me a serialization error \$\endgroup\$ – Ricardo Andres Rodriguez Gambo Jul 4 '18 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.