MMT
July 21, 2017, 9:03pm
1
Hi, I want to save and load correct file depending on sene loaded but I dont know how to include scene name to these lines bolded?
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
[B] FileStream file = File.Create (Application.persistentDataPath + "/car.rec");[/B]
bf.Serialize(file, data);
file.Close ();
}
public void Load()
{
[B]if (File.Exists (Application.persistentDataPath + "/car.rec")) [/B]
{
BinaryFormatter bf = new BinaryFormatter ();
[B]FileStream file = File.Open (Application.persistentDataPath + "/car.rec" , FileMode.Open);[/B]
file.Close ();
}
}
Use GetActiveScene() and then grab the name.
SceneManager.GetActiveScene().name;
1 Like
MMT
July 27, 2017, 7:48pm
3
Thanks Brathnann for you answer. I know ho to grab the name. The Problem is how to add it to FileStream file = File.Create (Application .persistentDataPath + “/car.rec”); as I dont know how to add any variables to the name.
Well, you could assign the name to a string variable. But you could just as easily include it in your File.Create call
FileStream file = File.Create(Application.persistentDataPath + "/" + SceneManager.GetActiveScene().name + ".rec");
//or
string sceneName = SceneManager.GetActiveScene().name;
FileStream file = File.Create(Application.persistentDataPath + "/" + sceneName + ".rec");
//or
string path = Application.persistentDataPath + "/" + SceneManager.GetActiveScene().name + ".rec"
FileStream file = File.Create(path);
2 Likes
TSC
July 27, 2017, 9:23pm
5
I assigned each scene it’s own int such as … lastSavedGame… save it separate from the game files and load and save every time when load or asave is called…
TSC
July 27, 2017, 9:25pm
6
On Start, run a case int(LastSaved)… int(LastSaved) … int(LastSaved) … Depending on the lastSaved(int) load lastSaved(file)
MMT
July 27, 2017, 9:40pm
7
thank you! It was that “/” that had to be moved to the beginning of the filename and after that I could add variable value.