Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Brackeys saving system

I looked up for a saving system in Unity and again ofcourse a Brackeys video showed up so I started watching it knowing that it will work and stuff. In case you don't know the video this is: https://www.youtube.com/watch?v=XOjd_qU2Ido

Know I've stumble upon a problem when using this. In the script it wants to get as reference a script where to take the values and store them in a binary file which is good. Now the problem is that I can run the functions Load and Save only inside the script where I hold those values. Is there any way of accesing that data from the binary file from another script/scene?

This is Brackeys script to save and load stuff using the binary fileusing System.IO;

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

public static class SavingSystem
{
    public static void SavePlayer(MMScript script)
    {
        BinaryFormatter formatter = new BinaryFormatter();

        string path = Application.persistentDataPath + "/data.fallen";

        FileStream stream = new FileStream(path, FileMode.Create);

        MMData data = new MMData(script);

        formatter.Serialize(stream, data);
        stream.Close();
    }

    public static MMData LoadPlayer()
    {
        string path = Application.persistentDataPath + "/data.fallen";
        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            MMData data = formatter.Deserialize(stream) as MMData;
            stream.Close();

            return data;
        }
        else
        {
            Debug.Log("Save Data not found");
            return null;
        }
    }

This is where it gets its values from (also from Brackeys)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class MMData
{
    public float mixerVolume;
    public int isMuted;

    public MMData (MMScript player)
    {
        mixerVolume = player.mixerVolume;
        isMuted = player.isMuted;
    }

And these two are the load and save functions

   public void SaveData()
    {
        mixerVolume = musicSlider.value;
        SavingSystem.SavePlayer(this);
    }
    public void LoadData()
    {
        MMData data = SavingSystem.LoadPlayer();
        if (data != null)
        {
            musicSlider.value = data.mixerVolume;
            isMuted = data.isMuted;
        }
        else
        {
            musicSlider.value = 1;
            isMuted = 0;
        }

    The load and save data are called in the same script and in short I want to know how to call these two functions from other scripts/scenes.

Sign In or Register to comment.