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 manuelmangual · Aug 23, 2016 at 07:40 AM · c#jsonfile-ioserializable

Issues saving file in iOS

I am getting this issue when I try to save my game in iOS

 UnauthorizedAccessException: Access to the path "/var/mobile/Containers/Bundle/Application/E5970DAC-7463-4F12-A96B-1A9D524F3459/Documents" is denied.
   at GameData.CreateFile () [0x00000] in <filename unknown>:0 
   at GameData.Save (Int32 _collectibleCount, Int32 _starCount, Int32 _minutes, Boolean _isLocked) [0x00000] in <filename unknown>:0 
   at UnityEngine.Events.UnityAction.Invoke () [0x00000] in <filename unknown>:0 
   at UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0 
   at MenuItmBehavior+<WaitForAnimation>c__Iterator13.MoveNext () [0x00000] in <filename unknown>:0 
   at UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) [0x00000] in <filename unknown>:0 
  
 (Filename: currently not available on il2cpp Line: -1)

I have no idea why I am getting this. Here is the code I am using to save/load my file:

 using UnityEngine;
 using System;
 using System.IO;
 using UnityEngine.SceneManagement;
 using System.Collections.Generic;
 
 public class GameData: MonoBehaviour
 {
     private static GameData _instance;
 
     public static GameData Instance
     {
         get
         {
             if (_instance == null)
             {
                 var go = new GameObject("GameData");
                 _instance = go.AddComponent<GameData>();
                 DontDestroyOnLoad(go);
             }
             return _instance;
         }
     }
 
     string _FileLocation;
     LevelCompleted _levelData;
 
     void Awake()
     {
 
 #if UNITY_IPHONE
         string fileNameBase = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/'));
         _FileLocation = fileNameBase.Substring(0, fileNameBase.LastIndexOf('/')) + "/Documents/" + "SaveData.json";
         Debug.Log(_FileLocation);
 #elif UNITY_ANDROID
         _FileLocation = Application.persistentDataPath + "/" + ""SaveData.json""; 
 #else
         _FileLocation = Application.dataPath + "/" + "SaveData.json"; 
 #endif
 
         // we need soemthing to store the information into 
         _levelData = new LevelCompleted();
     }
 
     public void Load()
     {
         // notice how I use a reference to type (UserData) here, you need this 
         // so that the returned object is converted into the correct type 
         _levelData = LoadData();
     }    
 
     public void Save(int _collectibleCount, int _starCount, int _minutes, bool _isLocked)
     {
         var level = new Level();
 
         _levelData.Level.Add(level);
 
         level.LevelName = SceneManager.GetActiveScene().name;
         level.CollectibleCount = _collectibleCount;
         level.GoldStars = _starCount;
         level.Minutes = _minutes;
         level.NextLevelLocked = _isLocked;
         level.lastLevelPlayed++;
         // This is the final resulting JSON from the serialization process 
         CreateFile();
     }
 
     // Finally our save and load methods for the file itself 
     void CreateFile()
     {
         if (!Directory.Exists(Path.GetDirectoryName(_FileLocation)))
             Directory.CreateDirectory(Path.GetDirectoryName(_FileLocation));
 
         if (File.Exists(_FileLocation))
         {            
             File.Delete(_FileLocation);
         }
         File.WriteAllText(_FileLocation, JsonUtility.ToJson(_levelData, true));
        
         Debug.Log("File written.");
     }
 
     public LevelCompleted LoadData()
     {
         if (File.Exists(_FileLocation))
         {
             var tr = File.ReadAllText(_FileLocation);
 
             var jsonObj = JsonUtility.FromJson<LevelCompleted>(tr);
             return jsonObj;
             
         }
         return null;
     }
 }
 
 [Serializable]
 public class PlayerSkins
 {
     public string name;
     public bool unlocked;
 }
 
 [Serializable]
 public class LevelCompleted
 {
     public List<Level> Level = new List<Level>();
 }
 
 [Serializable]
 public class Level
 {
     public string LevelName;
     public int CollectibleCount;
     public int GoldStars;
     public int Minutes;
     public bool NextLevelLocked;
     public int lastLevelPlayed;
 }


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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Constructing object array with JsonUtility 1 Answer

Best way(s) to handle Custom Item Databases 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to import Long and Lat (GPS Info) from a flat file and render the points. 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