I have two scene : Main Menu, Game
On the Main Menu I have also a two buttons : Save,Load
And two scripts :
Save System :
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem
{
public static void SavePlayer(Player player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.bin";
FileStream stream = new FileStream(path, FileMode.Create);
PlayerData data = new PlayerData(player);
formatter.Serialize(stream, data);
stream.Close();
}
public static PlayerData LoadPlayer()
{
string path = Application.persistentDataPath + "/player.bin";
if(File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PlayerData data = formatter.Deserialize(stream) as PlayerData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in " + path);
return null;
}
}
}
And Player :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public void SavePlayer()
{
SaveSystem.SavePlayer(this);
}
public void LoadPlayer()
{
PlayerData data = SaveSystem.LoadPlayer();
Vector3 position;
position.x = data.position[0];
position.y = data.position[1];
position.z = data.position[2];
transform.position = position;
}
}
The Player script is attached to the Player in the Game scene but now I can’t call or get access to the SavePlayer and LoadPlayer from the two buttons in the On Click events.
And another problem is that the Game scene is not all the time in the Hierarchy the Game scene is loaded when I click the Play button and then the Main Menu scene is not loaded and when I hit the escape key it’s loading the Main Menu scene but then the Game scene is not loaded.
So I wonder how should I and how can I make a reference between the buttons Save/Load and the functions SavePlayer/LoadPLayer ?
