I made a script for saving my game, but when I try to load the scene that it has “Saved” I get an error that says “UnauthorizedAccessException: Access to the path (my file path) is denied.” I think this may be because I never specified an actual name for my save file in the script, but I can’t figure out how. Here is my save script:
using System.IO;
public class SaveGame : MonoBehaviour
{
public string SceneName;
void save()
{
using (StreamWriter saveFile = File.CreateText(Application.dataPath + "/saves"))
saveFile.WriteLine(SceneName);
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
if (!Directory.Exists(Application.dataPath + "/save"))
{
Directory.CreateDirectory(Application.dataPath + "/save");
save();
}
else
{
save();
}
}
else
{
//do nothing
}
}
}
and here is the load script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.IO;
public class LoadGameBtn : MonoBehaviour
{
public void onClick()
{
using (FileStream fs = new FileStream(Application.dataPath + "/save", FileMode.Open))
{
using (StreamReader sr = new StreamReader(fs))
{
while (sr.Peek() >= 0)
{
SceneManager.LoadScene(sr.ReadLine());
}
}
}
}
}