• 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 cannolade · Nov 16, 2019 at 08:58 PM · iosserializationsave datatroubleshootingpersistentdatapath

Serialize Data on iOS in persistentDataPath

Hi all,

I have this code to save/load data of my players. It works perfectly in the unity editor but it doesn't work on my iphone 7. It seems that it creates and reads the file "player.fun" but I can't read / write the values. I have a GameObject with the script Player that is persistent between scenes.

Can you give me some clue what I am not doing correctly?

Thanks a lot!!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class PlayerData {
 
     public int level;
     public int credits;
     public int plays;
     public int extraballs;
     public int purchases;
     public float spent;
     public int music;
     public bool fx;
     public string avatar;
     public string language;
 
     public PlayerData (Player player)
     {
         level = player.level;
         credits = player.credits;
         plays = player.plays;
         extraballs = player.extraballs;
         purchases = player.purchases;
         spent = player.spent;
         music = player.music;
         fx = player.fx;
         avatar = player.avatar;
         language = player.language;
     }
 }

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : MonoBehaviour {
 
     public int level = 1;
     public int credits = 1500;
     public int plays = 0;
     public int extraballs = 0;
     public int purchases = 0;
     public float spent = 0.0f;
     public int music = 3;
     public bool fx = true;
     public string avatar = "horseshoe";
     public string language = "spanish";
 
     void Awake ()
     {
         #if UNITY_IOS
         // Forces a different code path in the BinaryFormatter that doesn't rely on run-time code generation (which would break on iOS).
         Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
         #endif
         LoadPlayer();
     }
 
     public void SavePlayer ()
     {
         SaveSystem.SavePlayer(this);
     }
 
     public void LoadPlayer ()
     {
         PlayerData data = SaveSystem.LoadPlayer();
 
         level = data.level;
         credits = data.credits;
         plays = data.plays;
         extraballs = data.extraballs;
         purchases = data.purchases;
         spent = data.spent;
         music = data.music;
         fx = data.fx;
         avatar = data.avatar;
         language = data.language;
     }
 
     #region UI Methods
 
     public void ChangeLevel (int amount)
     {
         level += amount;
     }
 
     public void ChangeCredits (int amount)
     {
         credits += amount;
     }
 
     public void ChangePlays (int amount)
     {
         plays += amount;
     }
 
     public void ChangeExtraballs (int amount)
     {
         extraballs += amount;
     }
 
     public void ChangePurchases (int amount, float dollars)
     {
         purchases += amount;
         spent += dollars;
     }
 
     public void ChangeMusic (int amount)
     {
         music += amount;
     }
 
     public void ChangeFx ()
     {
         if (fx == true)
         {
             fx = false;
         }
         else
         {
             fx = true;
         }
     }
 
     public void ChangeAvatar ()
     {
         if (avatar == "horseshoe")
         {
             avatar = "girl";
         }
         else if (avatar == "girl")
         {
             avatar = "boy";
         }
         else if (avatar == "boy")
         {
             avatar = "horseshoe";
         }
     }
 
     public void ChangeLanguage ()
     {
         if (language == "spanish")
         {
             language = "english";
         }
         else if (language == "english")
         {
             language = "portuguese";
         }
         else if (language == "portuguese")
         {
             language = "spanish";
         }
     }
 
     #endregion
 }

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




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

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

228 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 Game Data to iOS Device 1 Answer

problem with loading data with serialization in the right time 0 Answers

Android PersistentDataPath UnauthorizedAccessException 0 Answers

Serialize gameobject children behaviours 0 Answers

IOS File Path for saving data 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