Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by valentin56610 · Sep 14, 2019 at 08:59 PM · serializationnot workingscene-switchingsingletondeserialization

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 ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

190 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot read past end of stream, MemoryStream 1 Answer

Deserialize an outside object in Unity 0 Answers

Null Exception while serialize json string into txt file 0 Answers

Object reference not set to an instance at Compilation step ? 0 Answers

How to change Music between Scenes 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges