How to save Player's progress with serialized variables?

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 ();



}

so far as i can tell, your not actually setting the static variable “GameState.current” anywhere in the code that you provided. You decalared it, but you haven’t assigned it any value, so it is null. Unless of course there is another script you haven’t linked that actually assigns a GameState to GameState.current.

I have added this to my gamestate class and now my “GameState.current” is not null anymore:

void Awake(){

        current = this.txtscore;

    }

However, Unity throws a SerializationException. Why can this be?

You cannot serialize anything that belongs to Unity (Such as GameObjects, AudioSource, Text etc.) via C# BinaryFormatter by default.

I would suggest you to create a “container” for the data you want to serialize, and then serialize it only.

[System.Serializable]
public class Data {
  public int SomeInt;
  public bool SomeBool;
 
  // ....
  // Value types and classes marked as System.Serializable
}

If you want to automate process of serializing such data as Vector3, Quaternion etc. I suggest looking into Surrogates. Here’s a good example how to set them up:
https://answers.unity.com/questions/956047/serialize-quaternion-or-vector3.html

I’m just trying to save my progress on a local file. I’ve tried JSON method but can’t load and gather the data back. What’s the best way to pull this off?

I use BinaryFormatter with Surrogates for storing player local save data.