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 /
avatar image
0
Question by lTimesl · Mar 05, 2019 at 04:34 PM · system.io

loading file from instantiate button problem?

trying to create save and load system with instantiate button the problem this the file saved the info but when i try to load the savedData nothing change the Data wont load my knowledge lvl wouldn't help me in this to figure out

     public void savedfiles(Cube player)
     {
         string folderPath = Path.Combine(Application.persistentDataPath, foldername);
         filesavename = savename.text;
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(folderPath + "/" + filesavename + fileextnison);
         PData playerdata = new PData(player);
         bf.Serialize(file, playerdata);
         file.Close();
     }
     public void loaded()
     {
         string folderPath = Path.Combine(Application.persistentDataPath, foldername);
         string[] filePaths = Directory.GetFiles(folderPath, "*.txt");
         foreach (string g in filePaths)
         {
             var onlyFileName = Path.GetFileNameWithoutExtension(g);
             GameObject button = (GameObject)Instantiate(buttonprefab);
             button.GetComponentInChildren<Text>().text = onlyFileName;
             button.transform.SetParent(menupanle,false);
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(g, FileMode.Open);
             PData playerdata = (PData)bf.Deserialize(file);
             Debug.Log(onlyFileName);
         }

void Start() {

     string folderPath = Path.Combine(Application.persistentDataPath, foldername);
     if (!Directory.Exists(folderPath))
         Directory.CreateDirectory(folderPath);

} }

Comment
Add comment · Show 2
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 Tsaras · Mar 05, 2019 at 04:42 PM 0
Share

Have you checked if the 'g' variable holds the complete path to the file or just the filename?

I just also noticed that you are assigning the deserialized data to a local variable that will be lost when the function returns.

avatar image lTimesl Tsaras · Mar 05, 2019 at 05:15 PM 0
Share

i check that from debug.log and its hold the complete path.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by lTimesl · Mar 06, 2019 at 11:31 AM

i fix the problem it was in the end of the load function i forget to close the file using file.clos() this is my updated code its work perfectly just need the save and load checks like empty filenames etc hope this help any one search for simple multi save and load system; public class SaveSystem : MonoBehaviour { public InputField savename; public GameObject buttonprefab; public Transform menupanle; const string foldername = "Save12"; const string fileextnison = ".txt"; string filesavename;

     void Start()
     {
 
         string folderPath = Path.Combine(Application.persistentDataPath, foldername);
         if (!Directory.Exists(folderPath))
             Directory.CreateDirectory(folderPath);
     }
 
     public void SaveData1(Move Player)
     {
       string folderPath = Path.Combine(Application.persistentDataPath, foldername);
       filesavename = savename.text;
       BinaryFormatter bf = new BinaryFormatter();
       FileStream file = File.Create(folderPath+"/"+ filesavename +  fileextnison);
       PlayerDate Data = new PlayerDate(Player);
       bf.Serialize(file, Data);
       file.Close();
     }
 
     public void InstantiateButton(Move Player)
     {
         string folderPath = Path.Combine(Application.persistentDataPath, foldername);
         string[] filePaths = Directory.GetFiles(folderPath, "*.txt");
         foreach (string g in filePaths)
         {
 
             var onlyFileName = Path.GetFileNameWithoutExtension(g);
             GameObject button = (GameObject)Instantiate(buttonprefab);
             button.GetComponentInChildren<Text>().text = onlyFileName;
             button.GetComponent<Button>().onClick.AddListener(
                 () => { loading(g, Player); });
             button.transform.SetParent(menupanle, false);
             
 
         }
     }
     public void loading(string S,Move Player)
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Open(S, FileMode.Open);
         PlayerDate Data = (PlayerDate)bf.Deserialize(file);
         file.Close();
         Player.score = Data.score;
         Player.level = Data.level;
         Vector3 postaion;
         postaion.x = Data.posation[0];
         postaion.y = Data.posation[1];
         postaion.z = Data.posation[2];
         Player.transform.position = postaion;
     }
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

101 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

Related Questions

File.Move wont work , error. IOException: Win32 IO returned ERROR_ALREADY_EXISTS. Path: 1 Answer

System.IO.FileInfo 1 Answer

Getting Multiple images using File.ReadAllBytes take a long time and Higher Cpu Usage..? 0 Answers

Gain access to directory 1 Answer

Highscores in text file 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