using System.Collections;
using System.Collections.Generic;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine;
public class DataManegement : MonoBehaviour
{
public static DataManegement datamanegement;
//my variables
public int tokensHighScore;
public int TotalTokensCollcted;
public int AllCrystals;
private bool Buyed_L = false; // here i need to save that in the data and in the load of my data if he buy the Buyed_L = true but in my data because he was delete if he exit the shop scene or scene loaded how i do it that my datamanagement code
void Awake()
{
Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
datamanegement = this;
DontDestroyOnLoad (gameObject);
}
public void SaveData()
{
BinaryFormatter binFrom = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "gameInfo.dat");
gameData data = new gameData();
data.tokensHighScore = tokensHighScore;
data.TotalTokensCollcted = TotalTokensCollcted;
data.AllCrystals = AllCrystals;
binFrom.Serialize(file, data);
file.Close();
}
public void LoadData()
{
if (File.Exists(Application.persistentDataPath + "gameInfo.dat"))
{
BinaryFormatter binForm = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "gameInfo.dat", FileMode.Open);
gameData data = (gameData)binForm.Deserialize(file);
file.Close();
tokensHighScore = data.tokensHighScore;
TotalTokensCollcted = data.TotalTokensCollcted;
AllCrystals = data.AllCrystals;
}
}
}
[Serializable]
class gameData
{
public int tokensHighScore;
public int TotalTokensCollcted;
public int AllCrystals;
}
You english is even worse than mine! But i think i understand what you need. You should read and learn what’s the DontDestroyOnLoad ()
void Awake()
{
DontDestroyOnLoad(gameObject);
}
If you use this command in a script, the GameObject containing this script, the gameobject and all its components will be not destroyed when changing scene. This way you can “save” the state of the variables of the script.
In my projects, i normally create a GameController Object, which contains all the information about the player progress through the game.