My singleton is getting reinitialized when I change scene
Hi,
So I have two singletons, my player and my GameData.
I would like to know why my singleton is getting reinitialized when I change scene.
And no, it is not linked to the GameObject or whatever I have read on the net, because my player follows the same pattern and everything is working with him.
When I save on ApplicationQuit everything is saved correctly. When I load everything is loaded correctly. But then it's not working.
Here is my GameData class :
 using System;
 
 [Serializable]
 public class GameData
 {
     public Player player;
     public Enemy enemy;
 
     public static GameData _gameData = null;
     public static readonly object _myLock = new object();
 
     public static GameData Instantiate()
     {
         if (_gameData is null)
         {
             lock (_myLock)
             {
                 if (_gameData is null)
                 {
                     _gameData = new GameData();
                 }
             }
         }
         return _gameData;
     }
 }
And here is the script that is in my main menu scene :
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 
 public class StartClass : MonoBehaviour
 {
     GameData gameData;
     Player player;
 
     string filePath;
 
     private void Start()
     {
         gameData = GameData.Instantiate();
         player = Player.Instantiate();
 
         filePath = Application.persistentDataPath + "/GameData.sav";
     }
 
     public void OnStart()
     {
         BinaryFormatter bf = new BinaryFormatter();
 
         if (File.Exists(filePath))
         {
             File.Delete(filePath);
         }
 
         SceneManager.LoadScene("00");
     }
 
     public void OnLoad()
     {
         if (File.Exists(filePath))
         {
             FileStream file = File.Open(filePath, FileMode.Open);
             BinaryFormatter bf = new BinaryFormatter();
 
             gameData = (GameData)bf.Deserialize(file);
             player = gameData.player;
             file.Close();
 
             SceneManager.LoadScene(player.Scene);
         }
     }
 }
and here is the script that is on every map of the game : using UnityEngine;
 public class AllMap : MonoBehaviour
 {
     GameObject playerObj;
 
     SaveLoad saveLoad;
 
     private void Awake()
     {
         playerObj = GameObject.Find("Player");
 
         GameData gameData = GameData.Instantiate();
         Debug.Log(gameData.player.PosX);
 
         if (saveLoad.CheckPath())
         {
             Player player = Player.Instantiate();
             player = gameData.player;
             Vector3 vector3 = new Vector3(player.PosX, player.PosY);
             playerObj.transform.position = vector3;
         }
     }
 
     private void OnApplicationQuit()
     {
         Player player = Player.Instantiate();
         GameData gameData = GameData.Instantiate();
         gameData.player = player;
         saveLoad.Save();
     }
 }
the problem is when I debug.log after I deserialized the GameData, the player is perfectly loaded. But on next scene, the GameData is like empty, for what reasons ? Is there something I ignore ?
Answer by valentin56610 · Sep 14, 2019 at 09:33 PM
Don't pay attention to the binary formatter in the start function it should not be here. I deleted it.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                