• 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 /
This question was closed Aug 04, 2020 at 11:50 AM by gf457767 for the following reason:

This is outdated for me now

avatar image
0
Question by gf457767 · May 23, 2020 at 01:19 PM · save datasaveloadloading file

Save/Load System not working

I tried to create a save/load system for my project,but is not working.I put this function:Debug.Log("Data has been saved"); to tell me if when I press the save button the data I am really saving the data,and the in the console the message:Data has been saved appears,but when I click the load button the message(I created this message using the debug.log):Data has been loaded appears,but it is not loading(Data that I want to be saved and loaded are the player's level and the position of the player). Here are my codes:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class PlayerData {
 
  public int level;
  public float[] position;
 
  public PlayerData (DataSaved player) 
  { 
    level = player.level;
    position = new float[3];
    position[0] = player.transform.position.x;
    position[1] = player.transform.position.y;
    position[2] = player.transform.position.z;
  }
 }

 using UnityEngine;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 
 public static class SaveSystem {
 
   public static void SavePlayer (DataSaved player) 
   { 
     BinaryFormatter formatter = new BinaryFormatter();
     string path = Application.persistentDataPath + "/SavedData.dat";
     FileStream stream = new FileStream(path, FileMode.Create);
 
     PlayerData data = new PlayerData(player);
 
     formatter.Serialize(stream, data);
     stream.Close();
   }
 
   public static PlayerData LoadPlayer () 
   { 
     string path = Application.persistentDataPath + "/SavedData.dat";
     if(File.Exists(path)) 
     { 
       BinaryFormatter formatter = new BinaryFormatter();
       FileStream stream = new FileStream(path, FileMode.Open);
       PlayerData data = formatter.Deserialize(stream) as PlayerData;
       stream.Close();
 
       return data;
     } else 
     { 
       Debug.LogError("Saved file not found in " + path);
       return null;
     }
   }
 }

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class DataSaved : MonoBehaviour
 {
   public int level = 5;
   public Text text1;
   public Text text2;
   public Text text3;
   public Text text4;
 
   public void SavePlayer() 
   { 
     SaveSystem.SavePlayer(this);
     string timeUS = System.DateTime.UtcNow.ToLocalTime().ToString("M/d/yy  hh:mm:ss tt");
     text1.text = timeUS;
     text2.text = timeUS;
     text3.text = timeUS;
     text4.text = timeUS;
     Debug.Log("Data has been saved");
   }
 
   public void LoadPlayer() 
   { 
     PlayerData data = SaveSystem.LoadPlayer();
     level = data.level;
     Vector3 position;
     position.x = data.position[0];
     position.y = data.position[1];
     position.z = data.position[2];
     transform.position = position;
     Debug.Log("Data has been loaded");
   }
 }
 

  

 


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

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

126 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

Related Questions

Saving and loading different object types generated at runtime using JSON 0 Answers

how to have save and load system. With this programing,How to do a save and load system on mobile. With this programing 1 Answer

PlayerPrefs not saving in build 1 Answer

What is the code to load a panel that is not a buildIndex scene? 1 Answer

Saving And Loading Linked List Game Stats 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