- Home /
The question is answered, right answer was accepted
Application.persistentDataPath not working
I am trying to create a save file but when I try to reference Application.persistentDataPath it says UnityEngine.Application does not contain a definition for persistentDataPath. I have UnityEngine included. Here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO; public class GameController : MonoBehaviour { public static GameController controller; public float maxHealth; public bool opened = false; void Awake () { if(controller == null){ DontDestroyOnLoad(gameObject); controller = this; Load(); } else if(controller != this){ Destroy(gameObject); } if(opened == false){ opened = true; maxHealth = 3; } } void Update () { if(Input.GetAxis("Exit") > 0){ Save(); Application.Quit(); } } public void Save(){ if(File.Exists(Application.persistantDataPath + "/playerInfo.dat")){ BinaryFormatter bfread = new BinaryFormatter(); BinaryFormatter bfwrite = new BinaryFormatter(); FileStream fileread = File.Open("file:" + Application.persistentDataPath + "/playerInfo.dat", FileMode.Open); PlayerData dataread = (PlayerData)bfread.Deserialize(fileread); dataread.maxHealth = maxHealth; dataread.opened = opened; PlayerData datawrite = new PlayerData(); datawrite.maxHealth = maxHealth; datawrite.opened = opened; bfwrite.Serialize(fileread, datawrite); fileread.Close(); } else{ BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat"); PlayerData data = new PlayerData(); data.maxHealth = maxHealth; data.opened = opened; bf.Serialize(file, data); file.Close(); } } public void Load(){ if(File.Exists(Application.persistantDataPath + "/playerInfo.dat")){ BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open); PlayerData data = (PlayerData)bf.Deserialize(file); file.Close(); maxHealth = data.maxHealth; opened = data.opened; } } [Serializable] class PlayerData { public float maxHealth; public bool opened; } }
Please tell me what I am doing wrong and how to fix it.
Answer by AaronXRDev · Nov 09, 2018 at 07:01 PM
In your code there are a couple spelling errors:
Application.persistantDataPath //Note the "a" in "persistant"
vs.
Application.persistentDataPath //Should be an "e"