[英]FileNotFoundException when trying to Load Game Data from save file
I am unsure why I am getting this error;我不确定为什么会收到此错误;
FileNotFoundException: Could not find file "........\\New Unity Project\\Assets\\Saves\\NewGame" FileNotFoundException:找不到文件“........\\New Unity Project\\Assets\\Saves\\NewGame”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
[System.Serializable]
public class PlayerStatistics : MonoBehaviour
{
private string _loadGameName;
//all save variables, only leaving one example
private int _decision1;
public void ChangeGameLoadName(string LoadGameName)
{
_loadGameName = LoadGameName;
Debug.Log("Load Game Name Received by Player Stats as : " + LoadGameName) ;
}
public void PlayerLoadGameData()
{
string LoadName = _loadGameName;
Debug.Log("Attempting to load game with the filename: " + LoadName);
BinaryFormatter loadGameBF = new BinaryFormatter();
string ApplicationFolder = Application.dataPath;
string saveGameDirectory = ApplicationFolder + "/Saves/";
FileStream loadFile = File.Open(saveGameDirectory + LoadName, FileMode.Open);
Save load = (Save)loadGameBF.Deserialize(loadFile);
loadFile.Close();
_decision1 = load._decision1;
//other save vars here
Debug.Log("GameInfoLoaded in player stats");
TakeLoadGameAndPlay();
}
}
The Debug.Log in ChangeGameLoadName is printing the correct name eg Load Game Name Received by Player Stats as : Google.save ChangeGameLoadName 中的 Debug.Log 正在打印正确的名称,例如 Load Game Name Received by Player Stats as : Google.save
The "Attempting to load game with the filename:" prints as NewGame. “正在尝试使用文件名加载游戏:”打印为 NewGame。 For reference, the user presses a button which invokes ChangeGameLoadName(), then another button which invokes PlayerLoadGameData().
作为参考,用户按下一个调用 ChangeGameLoadName() 的按钮,然后按下另一个调用 PlayerLoadGameData() 的按钮。 I don't know where it is getting this "NewGame" from at all.
我根本不知道它是从哪里得到这个“NewGame”的。 Any help would be very appreciated.
任何帮助将不胜感激。
EDIT:编辑:
ChangeGameLoadName() is called from my GameManager script through this function: ChangeGameLoadName() 是通过这个函数从我的 GameManager 脚本调用的:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
// Script References
GameView GameViewScript;
PlayerStatistics PlayerStatisticsScript;
GameObject GlobalObject;
PlayerOptions PlayerOptionsScript;
PlayerInput PlayerInputScript;
StoryHandler StoryHandlerScript;
GameHUD GameHUDScript;
[SerializeField]
private GameObject _storyScripts;
private float _shortFadeSpeed = 0.75f;
private float _longFadeSpeed = 1.5f;
private float _longLongFadeSpeed = 3f;
private void Start()
{
GameViewScript = GetComponent<GameView>();
PlayerInputScript = GetComponent<PlayerInput>();
StoryHandlerScript = _storyScripts.GetComponent<StoryHandler>();
GlobalObject = GameObject.FindGameObjectWithTag("GlobalScripts");
PlayerStatisticsScript = GlobalObject.GetComponent<PlayerStatistics>();
PlayerOptionsScript = GlobalObject.GetComponent<PlayerOptions>();
GameHUDScript = GetComponent<GameHUD>();
}
public void LoadGameNameHUDtoPlayerStatistics(string LoadGameName)
{
PlayerStatisticsScript.ChangeGameLoadName(LoadGameName);
Debug.Log("Game Manager Received Name and Sent it to Player Stats. Name = "+ LoadGameName);
}
}
The above debug log prints correctly.上面的调试日志打印正确。 (as google.save if my savefilename was google.save)
(如果我的保存文件名是 google.save,则作为 google.save)
Which in turn was called from:反过来又是从以下位置调用的:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpecialPrefabLoad : MonoBehaviour
{
private GameManager GameManagerScript;
public void GetLoadGameNameAndFeedToLoad()
{
GameObject Canvas = GameObject.Find("Canvas");
GameManagerScript = Canvas.GetComponent<GameManager>();
string LoadGameName = this.gameObject.name;
GameManagerScript.LoadGameNameHUDtoPlayerStatistics(LoadGameName);
Debug.Log("Found Save Game and Sent Info to Game Manager");
}
}
This was a special script because its attached to a prefab button which instantiates the load game buttons.这是一个特殊的脚本,因为它附加到一个预制按钮上,该按钮实例化了加载游戏按钮。 I couldn't find an alternate way to do this.
我找不到替代方法来做到这一点。 This was called by the OnClick() Unity Function.
这是由 OnClick() Unity 函数调用的。
The gameObject.name was set here: gameObject.name 在此处设置:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.Audio;
using System.IO;
public class GameHUD : MonoBehaviour
{
[SerializeField]
private AudioMixer _globalAudio;
[SerializeField]
private GameObject _faderBackgroundObject;
[SerializeField]
private Image _faderBackground;
[SerializeField]
private GameObject _menuMainGame;
[SerializeField]
private GameObject _menuButtons;
[SerializeField]
private GameObject _saveGameMenu;
[SerializeField]
private GameObject _loadGameMenu;
[SerializeField]
private GameObject _optionsMenu;
[SerializeField]
private GameObject _audioOptions;
[SerializeField]
private GameObject _gameplayOptions;
[SerializeField]
private Slider _globalVolumeSlider;
[SerializeField]
private Slider _bgmVolumeSlider;
[SerializeField]
private Slider _fxVolumeSlider;
private float _longFadeSpeed = 1.5f;
private float _shortFadeSpeed = 0.5f;
[SerializeField]
private GameObject _canvas;
private GameManager GameManagerScript;
public Text inputText;
[SerializeField]
private GameObject _gameSavedNotification;
[SerializeField]
private GameObject _loadGameScroller;
[SerializeField]
private GameObject _prefabLoadGameButton;
private void Start()
{
GameManagerScript = _canvas.GetComponent<GameManager>();
SetGameSettingsFromPlayerPrefs();
}
private void GetPlayerLoadGameList()
{
string ApplicationFolder = Application.dataPath;
string[] saveFolder = Directory.GetFiles(@ApplicationFolder + "/Saves/", "*.save");
int i = 0;
foreach (string saveGameFile in saveFolder)
{
i = i + 1;
GameObject myNewObj = Instantiate(_prefabLoadGameButton, _loadGameScroller.transform);
myNewObj.transform.position = new Vector3(_loadGameScroller.transform.position.x + 250, _loadGameScroller.transform.position.y - (i * 20), 0);
myNewObj.name = saveGameFile;
Text saveGameName = myNewObj.GetComponent<Text>();
saveGameName.text = Path.GetFileNameWithoutExtension(saveGameFile);
myNewObj.name = saveGameName.text + ".save";
}
}
}
Bit old, but I thought I'd say.有点旧,但我想我会说。 It was an instance issue.
这是一个实例问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.