Saving/Loading

Hi,
This is my first time using the forums for help, so I will try to get to the point and hopefully, I am using the correct forum help section.
I have a Game Manager Script that Extends from a Singleton Script. Here is an example of that Manager script:

using UnityEngine;
using System.Collections;

public class GameManager : Singleton<GameManager> {



    //Player HP
    private float _playerHealth;
   
    public float PlayerHealth
    {
        get { return _playerHealth; }
        set { _playerHealth = value; }
    }

    //Player MP
    private float _playerMagic;
   
    public float PlayerMagic
    {
        get { return _playerMagic; }
        set { _playerMagic = value; }
    }



    //
    //Items for Getting and Setting:
    //


    //Number Sparkles
    private  int _numSparks;
   
    public int NumSparks {
        get { return _numSparks; }
        set { _numSparks = value; }
    }

    //Number Hearts
    private int _numHearts;
   
    public int NumHearts {
        get { return _numHearts; }
        set { _numHearts = value; }
    }

.
.
.[/code]

Taking an example from the script, I want to save the _numHearts value for permanence. But before I do that, another script accesses those values:

void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.gameObject.tag == "Player")
        {
            if(gameObject.name == "SparkleItem") {
                SparklesPickup();
            }
           
            else if (gameObject.name == "HeartItem") {
                HeartPickup();
            }
           
.
.
.

   
    //Picking up ALL in-game ITEMS. I.E. Coins, Sparkles, Mushrooms, Ore, ect.
   
    private void SparklesPickup()
    {
        GameManager.Instance.NumSparks++;
            SaveLoad.control.readSparks+=1;
        Destroy (gameObject);
    }
   
    private void HeartPickup()
    {
        GameManager.Instance.NumHearts++;
                SaveLoad.control.readHearts+=1;
        Destroy (gameObject);
    }
.
.
.

Then I have another script that is the UI output, and finally, I have the SaveLoad script:

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;


public class SaveLoad: MonoBehaviour {
   
    public static SaveLoad control; //can access from anywhere now <<
   
    public float health;
    //public float experience;

    //Attempt To save the Item --- Collects Between 2 levels: S

    public int readSparks;
    public int readHearts;
   
   
   
    void Awake () {
        if (control == null) {
            DontDestroyOnLoad (gameObject);
            control = this;
        }
       
        else if(control !=this) {
            Destroy (gameObject);
        }
    }
   


    ///
    /// Saving And Loading ///
   
    [Serializable] 
    class PlayerData {

        public float health;
        //public float experience;


        public int classSparks;
        public int classHearts;
    }
   
   
    public void Save() {
        BinaryFormatter bf = new BinaryFormatter ();
        FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");
        PlayerData mydata = new PlayerData ();

        mydata.health = health;
        //mydata.experience = experience;

        mydata.classSparks = readSparks;
        mydata.classHearts = readHearts;

       
        bf.Serialize (file, mydata);
        file.Close ();
       
    }
   
    public void Load() {
        if(File.Exists(Application.persistentDataPath + "/playerInfo.dat")) {
            BinaryFormatter bf = new BinaryFormatter();

            FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.dat", FileMode.Open); //<--file is a local var to Load
            PlayerData mydata = (PlayerData)bf.Deserialize(file);
            file.Close ();
           
            health = mydata.health;
            //experience = mydata.experience;

            readSparks = mydata.classSparks;
            readHearts = mydata.classHearts;
           
        }
    }

I shortened these scripts to post my question here, but I think the overall idea of what I’m trying to do is illustrated. How can I access the numHearts, for example, which is in the GameManager and store it in a container to write save data to, and later retrieve. I tried FindObjectOfType<>(); in the Start menu, and then assigning readHearts to = GAmeManager.Instance of _numHearts.

I am not sure how I should be handling this problem, actually. I have a hard time finding information on storing data to a binary file as it seems most tutorials cover Player Prefs. I tried using BinaryWriter too but I haven’t been able to make effective use out of it because I’m using it wrong. I just haven’t been able to figure it out yet.

Is someone able to notice what I’m doing wrong using this method? Otherwise I can re-explain or go into more detail.

Thanks!

I don’t know enough to answer your question, but perhaps this video might help you:
https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

I’ve been using it myself to try to learn about how to save/load data. If you’ve viewed this video already or it doesn’t help, I apologize.

Thanks :slight_smile: I did view it already.
I will try to answer more questions. Based on what I’ve wrote above, I am trying to save, among other things, the numHearts. My question is, how can I save an integer object(numHearts) that is created on script A, changes made to the value of the integer item(numHearts) on scipt B, communicated changes then made back to script A, and finally, on script C(a Save/Load script), how can I read that integer value(numHearts) over and assign it to a container value so that I can read and write it for saving to a binary save. I feel like I am on the brink of answering my own question, but nothing seems to work so far that I’ve tried. I’ve tried making static values and finding the associated scripts on script C(save/Load script).

I think I solved it. I am not completely sure yet but I put my save load script in my game manager. I made all items I want to save private static variables. So far it works for two different types of instances. I changed my manager so that it did not derive from the singleton too, because that was just creating problems. If anyone wants to talk about this topic with me, offer suggestions or has similar problems, I will check back on this thread.

Thanks.