How to include name of scene to Application.persistentDataPath

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

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

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…

On Start, run a case int(LastSaved)… int(LastSaved) … int(LastSaved) … Depending on the lastSaved(int) load lastSaved(file)

thank you! It was that “/” that had to be moved to the beginning of the filename and after that I could add variable value.