bobbypin
bobbypin

Reputation: 5

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"

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 e.g. Load Game Name Received by Player Stats as : Google.save

The "Attempting to load game with the filename:" prints as NewGame. For reference, the user presses a button which invokes ChangeGameLoadName(), then another button which invokes PlayerLoadGameData(). I don't know where it is getting this "NewGame" from at all. Any help would be very appreciated.

EDIT:

ChangeGameLoadName() is called from my GameManager script through this function:

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)

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.

The gameObject.name was set here:

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";
        }

    }
}

Upvotes: 0

Views: 634

Answers (1)

bobbypin
bobbypin

Reputation: 5

Bit old, but I thought I'd say. It was an instance issue.

Upvotes: 0

Related Questions