how i save that in my data in load or save

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;
}

Good day @Haeder2132 !

You english is even worse than mine! :smiley: 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.

@kaplica is also right, he says :

What you want is a singleton. It’s a class that is only instantiated once.

 public static DataManegement instance;
 
 private void Awake()
 {
       if(instance != null)
       {
             Destroy(gameObject);
       } else 
       {
             Instance = this;
             DontDestroyOnLoad(gameObject);
       }
 }

And if you want a generic one to use for any type of classes you can use this:

 public class GenericSingletonClass<T> : MonoBehaviour where T : Component
 {
     private static T instance;
     public static T Instance {
       get {
         if (instance == null) {
            instance = FindObjectOfType<T> ();
            if (instance == null) {
              GameObject obj = new GameObject ();
              obj.name = typeof(T).Name;
              instance = obj.AddComponent<T>();
            }
         }
        return instance;
       }
     }
  
     public virtual void Awake ()
     {
        if (instance == null) {
          instance = this as T;
          DontDestroyOnLoad (this.gameObject);
     } else {
       Destroy (gameObject);
     }
   }
 }

And then you can use it like this:

 public class MyGameScoreController : GenericSingletonClass<MyGameScoreController>; 
 {
   
 }

If this helps, Upvote & marke the answer !!

If need more help, give more info and ask for more using @tormentoarmagedoom

Bye :smiley: !

What you want is a singleton. It’s a class that is only instantiated once.

public static DataManegement instance;

private void Awake()
{
      if(instance != null)
      {
            Destroy(gameObject);
      } else 
      {
            Instance = this;
            DontDestroyOnLoad(gameObject);
      }
}

And if you want a generic one to use for any type of classes you can use this:

public class GenericSingletonClass<T> : MonoBehaviour where T : Component
{
    private static T instance;
    public static T Instance {
      get {
        if (instance == null) {
           instance = FindObjectOfType<T> ();
           if (instance == null) {
             GameObject obj = new GameObject ();
             obj.name = typeof(T).Name;
             instance = obj.AddComponent<T>();
           }
        }
       return instance;
      }
    }
 
    public virtual void Awake ()
    {
       if (instance == null) {
         instance = this as T;
         DontDestroyOnLoad (this.gameObject);
    } else {
      Destroy (gameObject);
    }
  }
}

And then you can use it like this:

public class MyGameScoreController : GenericSingletonClass<MyGameScoreController>; 
{
  
}