is there a variable that doesn’t reset each time an application is launched? closed

You can save the boolean value.

Here’s how you do it:

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

public class SaveSystem
{

// =====Saving=====

// "path" should be filled with the full path of the save directory, including the file name and the file extension.
    public static void Save(bool boolean, string path)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream stream = new FileStream(path, FileMode.Create);

        formatter.Serialize(stream, boolean);
        Debug.Log($"Bool saved at {path}");
        stream.Close();
    }


// =====Loading=====

// "path" should be filled with the full path of the save directory, including the file name and the file extension.
    public static bool LoadOptions(string path)
    {
        if(!File.Exists(path))
        {
            Console.WriteLine($"Options file not found in {path}"); //For debugging, is removable
            return false;
        }
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream stream = new FileStream(path, FileMode.Open);
        bool stuff = formatter.Deserialize(stream) as bool;
        Debug.Log($"Bool loaded at {path}");
        stream.Close();
        return stuff;
    }
}

Just make sure to load it on start. This saving method also works with any other thing, such as ints, and your own classes (<!> provided it has “[System.Serializable]” on top <!>, and you modify the type of data it saves/loads.)

[EDIT] This is one of many saving algorithms. This is a method of saving to a binary file. If you want to save to a text file instead, other answers may help. Keep in mind that binary files are harder to tamper with than text/xml files, so this is the recommended way to save things.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top