Hey,
I want to save some data of a trainer in a sports game in a json file.
For this, i have the following function:
public List<TrainerObjectData> ListOfObjects = new List<TrainerObjectData>();
public void saveList()
{
TrainerObjectData[] ObjectData = new TrainerObjectData[ListOfObjects.Count];
ListOfObjects.CopyTo(ObjectData, 0);
string JsonString = JSON_Helper.ToJson(ObjectData, true);
File.WriteAllText(Application.persistentDataPath + "/Save_Data/TrainerSave.json", JsonString);
}
The Code from the JSON_Helper is:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class JSON_Helper
{
[Serializable]
private class Wrapper<T>
{
public T[] Objekte;
}
public static string ToJson<T>(T[] array)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Objekte = array;
return JsonUtility.ToJson(wrapper);
}
public static string ToJson<T>(T[] array, bool pretty)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Objekte = array;
return JsonUtility.ToJson(wrapper, pretty);
}
public static T[] FromJson<T>(string json)
{
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.Objekte;
}
}
The Code of the TrainerObjectData is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[Serializable]
public class TrainerObjectData
{
public int LineID;
public float Speed;
public int Level;
public int Skin;
public TrainerObjectData(int LineID, int Level, float Speed, int Skin)
{
this.LineID = LineID;
this.Speed = Speed;
this.Level = Level;
this.Skin = Skin;
}
}
In the game I have a button that executes the “saveList()” function and a few other functions.
However, the button does not work on the iPhone. As soon as I comment out “saveList()”, the button works. But then, of course, nothing is saved.
In Editor, the function works fine, when i use File.WriteAllText(Application.dataPath + "/Save_Data/TrainerSave.json", JsonString);
What is wrong with the saveList()
function?
This is the error message from XCode:
DirectoryNotFoundException: Could not find a part of the path "/var/mobile/Containers/Data/Application/A14D5957-862E-42FD-90A7-5FDB7C9DDBFD/Documents/Save_Data/TrainerSave.json".
at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x00000] in <00000000000000000000000000000000>:0
at System.IO.StreamWriter..ctor (System.String path, System.Boolean append, System.Text.Encoding encoding, System.Int32 bufferSize) [0x00000] in <00000000000000000000000000000000>:0
at System.IO.File.WriteAllText (System.String path, System.String contents) [0x00000] in <00000000000000000000000000000000>:0
at Eingang_Upgrade.SetupTrainer (System.Int32 AnzTrainer, System.Int32 Level, System.Single Speed, System.Int32 Skin, System.Boolean pay) [0x00000] in <00000000000000000000000000000000>:0
at Eingang_Upgrade.Upgrade () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.Events.UnityEvent.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchPress (UnityEngine.EventSystems.PointerEventData pointerEvent, System.Boolean pressed, System.Boolean released) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchEvents () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.StandaloneInputModule.Process () [0x00000] in <00000000000000000000000000000000>:0
UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchPress(PointerEventData, Boolean, Boolean)
UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchEvents()
UnityEngine.EventSystems.StandaloneInputModule:Process()