problem with PlayerPrefs.SetString...

Hello Unityrs,

I build a chat line.. and to log in I fill up a textArea saving the value (the username) into a variable, let's call it playerNameInput.

For example: playerNameInput = GUI.TextArea (Rect (horzOffset+30, 80, 300 , 20), playerNameInput);

Just after that statement I call:

PlayerPrefs.SetString("playerName", playerNameInput); //Setting the player's name.

But somehow, when I display the content of "playerName" field in the chat room, I get always the previous one and never the current. So, I wonder when the PlayerPrefs are saved?! and How can I update PlayerPrefs at runtime.

Many thanks. Giancarlo

You should use

PlayerPrefs.GetString("playerName"); 

in order to read those data.

In order to save the preference you must use:

PlayerPrefs.Save

unity automatically saves upon application quit/ exit, this is why it is always showing the previous sessions names.
look
here

for more info.