Saving Stream to PlayerPref then unpack in new scene.

So maybe my methodology is a bit off, but this is what I’m trying to do.

  1. I’ve successfully created code to save my player data off using a BinaryFormatter. (Check)

  2. De-serialize said save. (also Check)

and here’s where i deviate from the norm. I’m trying to pass the saved info off to another scene that gets loaded by assigning all the saved info into the PlayerPrefs.SetString() method.

this is the block of code I’m using to do so:

void SaveToPref(Stream s)
    stream.Position = 0;
    StreamReader sr = new StreamReader(s);
    string buffer = sr.ReadToEnd();
    PlayerPrefs.SetString("_sav", buffer);

in theory this should place the stream into my playerpref to be unpacked later. When my new scene loads it will apply all my characters personl attributes like so;

    void UnPackPref()
        string sav = PlayerPrefs.GetString("_sav");
        byte[] loadStream = Encoding.UTF8.GetBytes(sav);
        MemoryStream ms = new MemoryStream(loadStream);
        BinaryFormatter bf = new BinaryFormatter();
        ps_ = (PlayerStats_)bf.Deserialize(ms);

but all the Debug.Log() files are telling me that my PlayerPref.GetString(“_sav”) are empty. It’s not properly converting the stream to a string like i want. Any ideas?

I didn’t know exactly what is the problem, but you use word binary and are doing direct conversions to string. This have a very bad smell.

To convert a byte array to a string use a proper encode like base64.

This tutorial could solve your problem.