Hello,
This time, I want to save the exact position of the player GameObject within the exact scene to a file, and then load it from a file. Here is what I have so far:
SaveManager.cs:
public class SaveManager : MonoBehaviour
{
public static string fileName;
public static string saveDirectory;
public PlayerController ball;
public float x;
public float y;
void Start()
{
ball = FindObjectOfType<PlayerController>();
x = ball.myBod.position.x;
y = ball.myBod.position.y;
}
public void Save()
{
saveDirectory = Application.persistentDataPath + "/saveFile.txt";
using (BinaryWriter writer = new BinaryWriter(File.Open(saveDirectory, FileMode.Create)))
{
writer.Write(HealthManager.playerHealth);
writer.Write(LivesSystem.lives);
writer.Write(ScoreSystem.score);
writer.Write(x);
writer.Write(y);
};
}
public void Load()
{
saveDirectory = Application.persistentDataPath + "/saveFile.txt";
if (File.Exists(saveDirectory))
{
using (BinaryReader reader = new BinaryReader(File.Open(saveDirectory, FileMode.Open)))
{
HealthManager.playerHealth = reader.ReadInt32();
LivesSystem.lives = reader.ReadInt32();
ScoreSystem.score = reader.ReadInt32();
x = reader.ReadSingle();
y = reader.ReadSingle();
}
}
}
}
GameManager.cs:
public class GameManager : MonoBehaviour
{
public PauseScreen ps;
public SaveManager sm;
void Start()
{
ps = FindObjectOfType<PauseScreen>();
sm = FindObjectOfType<SaveManager>();
}
public void Load()
{
sm.Load();
ps.Resume();
}
public void Save()
{
sm.Save();
}
}
Can someone please help with this? Any would be appreciated.
Sincerely,
burchland2