Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by burchland2 · Sep 12, 2021 at 10:08 PM · saveloadbinarywriter

Still cannot load a saved game

Hi, I'm trying to write a save/load class through BinaryWriter and BinaryReader. (I hope that method is secure enough.) I successfully saved the file, but I could not load it. Here is the relevant code:

SaveManager.cs:

 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using UnityEngine;
 
 public class SaveManager : MonoBehaviour
 {
     public static string directory = @"\SaveLoad";
     public static string fileName = @"\saveFile.sav";
     public static string savePath;
     public static string saveDirectory;
 
     public static void Save(Player player)
     {
         saveDirectory = Application.persistentDataPath + directory;
         savePath = saveDirectory + fileName;
 
         if (!DirectoryExists())
             Directory.CreateDirectory(saveDirectory);
 
         FileStream sr = File.Create(savePath);
         BinaryWriter bw = new BinaryWriter(sr);
 
         PlayerData data = PlayerData.FromPlayer(player);
         bw.Write(data.Health);
         bw.Write(data.Lives);
         bw.Write(data.Score);
 
         if (data.Position == null)
         {
             bw.Write(0);
         }
         else
         {
             bw.Write(data.Position.Length);
             foreach (var value in data.Position)
             {
                 bw.Write(value);
             }
         }
         sr.Close();
 
         Debug.Log("Your game has been saved.");
     }
 
     public static PlayerData Load()
     {
         saveDirectory = Application.persistentDataPath + directory;
         savePath = saveDirectory + fileName;
 
         if (!File.Exists(savePath))
         {
             Debug.LogError("Save file not found in " + savePath);
             return null;
         }
         FileStream sr = File.OpenRead(savePath);
         BinaryReader br = new BinaryReader(sr);
 
         PlayerData data = new PlayerData();
         data.Health = br.ReadInt32();
         data.Lives = br.ReadInt32();
         data.Score = br.ReadInt32();
 
         int length = br.ReadInt32();
         data.Position = new float[length];
         for (int index = 0; index < length; index++)
         {
             data.Position[index] = br.ReadSingle();
         }
         sr.Close();
         Debug.Log("Your game has been loaded.");
 
         return data;
     }
 
     private static bool SaveExists()
     {
         return File.Exists(GetFullPath());
     }
 
     private static bool DirectoryExists()
     {
         saveDirectory = Application.persistentDataPath + directory;
         return Directory.Exists(saveDirectory);
     }
 
     private static string GetFullPath()
     {
         saveDirectory = Application.persistentDataPath + directory;
         savePath = saveDirectory + fileName;
         return savePath;
     }
 }

PlayerData.cs:

 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class PauseScreen : MonoBehaviour
 {
     public GameObject PauseCanvas;
     
     public static bool GameIsPaused;
 
     public PlayerController ball;
 
     public Player player;
 
     void Start()
     {
         GameIsPaused = false;
         ball = FindObjectOfType<PlayerController>();
         player = FindObjectOfType<Player>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetButtonDown("Cancel"))
         {
             if (GameIsPaused)
             {
                 Resume();
             }
             else
             {
                 Pause();
             }
         }
     }
 
     public void Resume()
     {
         Time.timeScale = 1;
         GameIsPaused = false;
         PauseCanvas.SetActive(false);
     }
 
     public void Pause()
     {
         Time.timeScale = 0;
         GameIsPaused = true;
         PauseCanvas.SetActive(true);
     }
 
     public void MainMenu()
     {
         Resume();
         SceneManager.LoadScene("MainMenu");
     }
 
     public void StartScreen()
     {
         Resume();
         SceneManager.LoadScene("Intro");
     }
 
     public void Save()
     {
         player.Save();
     }
 }

Player.cs:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : MonoBehaviour
 {
     public int score = 0;
     public int lives = 9;
     public int health = 3;
 
     public void Save()
     {
         SaveManager.Save(this);
     }
 
     public void Load()
     {
         PlayerData data = SaveManager.Load();
         score = data.Score;
         lives = data.Lives;
         health = data.Health;
 
         Vector2 position;
         position.x = data.Position[0];
         position.y = data.Position[1];
         transform.position = position;
     }
 }

Can someone please tell me what I did wrong and/or what I am missing? Any assistance would be appreciated.

Sincerely, burchland2

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
1

Answer by Bunny83 · Sep 12, 2021 at 10:21 PM

Where or when do you actually call your Load method in your player class? Have you added some Debug.Logs to your code to verify which parts actually execute? We can not debug your code for you since we only know what you are telling us. The code you've shown never calls the Load method anywhere. So we can not verify it runs at all.

Comment
Add comment · Show 1 · 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
avatar image burchland2 · Sep 13, 2021 at 05:21 PM 0
Share

@Bunny83

Thanks for the suggestion.

I just called it in the SweetSpot class here,

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SweetSpot : MonoBehaviour
 {
     public Player player;
     
     // Start is called before the first frame update
     void Start()
     {
         player = FindObjectOfType<Player>();
     }
 
     public void Load()
     {
         player.Load();
     }
 }

and placed it in the appropriate game object in a button that belongs to a different scene in the game (SweetSpot). But as soon as I tested the game again, nothing happened other than the Debug.Log line. I verified that the saved file is present within the Application.persistentDataFile folder, but it just cannot be loaded back in.

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

128 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

Related Questions

Saving and loading data from file 0 Answers

Serialization scripting error 1 Answer

Saving Melee Combat Template Pack (MY LAST PROBLEM) 1 Answer

Saving GameObject to file 2 Answers

Best SAVE/LOAD system for my 4X Strategy hobby game? 1 Answer


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