PlayerPrefs script problem

Hi I’ve been at this all day an have gotten nowhere. I have a script for a message system and I’ve decides to use PlayerPrefs to save and load messages so they remain after the game has been closed and reopened. I get no script errors but for some reason when I close and open the app, posted messages are not reloaded. If anyone can tell me what’s wrong with what i’ve done I’d be so grateful.

Here’s the javascript code I’ve been working on:

var stringToEdit: String = "What's The News?";
var customSkin: GUISkin;
var maxMessage = 10;
var messagesTyped: String[];
var messageSpacing: float = 10;
var maxChar : int = 30;
var scrollPosition : Vector2 = Vector2.zero;
var messageAlert : String = "New Message From ";
var nameField: String = "Name";
private var msgCounter: int;


function Start(){
    maxMessage--;
    messagesTyped = new String[maxMessage];
    GetMessages();
}

function GetMessages(){
if (PlayerPrefs.HasKey(nameField) == true)
{
PlayerPrefs.GetString(nameField, "Name");
print("loaded");
}
}

function OnGUI(){
	GUI.skin = customSkin;	
    stringToEdit = GUI.TextField(Rect(270, 100, 450, 80), stringToEdit, maxChar);
    nameField = GUI.TextField(Rect(80, 100, 180, 30), nameField, maxChar);
    if (GUI.Button(Rect(730, 100, 60, 30), "Submit"))
    {
        SubmitMsg( messageAlert + nameField + ":  " + stringToEdit);
        SaveString();
    }
    var x: float = 270;
    var y: float = 180;
    for (var i: int;
    i < messagesTyped.length;
    i++)
    {
        y += messageSpacing;
        //print("Here");
        GUI.Label(Rect(x, y, 500, 60), messagesTyped*);*

}
}

function SubmitMsg(submittedMsg: String){
if (msgCounter == maxMessage)
{
msgCounter = 0;
}
messagesTyped[msgCounter] = submittedMsg;
print(messagesTyped[0]);
msgCounter++;
}

function SaveString(){
PlayerPrefs.SetString(nameField, “Name”);
print(“saved”);
}
Thanks again!

Without looking at your code yet, you do know there is a limit to how much you can store prefs, right? Is it possible you've gone beyond that limit?

2 Answers

2

PlayerPrefs.GetString returns the value from PlayerPrefs.

See
http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.GetString.html

Fixed code:

var stringToEdit: String = "What's The News?";
var customSkin: GUISkin;
var maxMessage = 10;
var messagesTyped: String[];
var messageSpacing: float = 10;
var maxChar : int = 30;
var scrollPosition : Vector2 = Vector2.zero;
var messageAlert : String = "New Message From ";
var nameField: String = "Name";
private var msgCounter: int;


function Start(){
    maxMessage--;
    messagesTyped = new String[maxMessage];
    GetMessages();
}

function GetMessages(){
if (PlayerPrefs.HasKey(nameField) == true)
{
nameField = PlayerPrefs.GetString("Name");
print("loaded");
}
}

function OnGUI(){
    GUI.skin = customSkin;  
    stringToEdit = GUI.TextField(Rect(270, 100, 450, 80), stringToEdit, maxChar);
    nameField = GUI.TextField(Rect(80, 100, 180, 30), nameField, maxChar);
    if (GUI.Button(Rect(730, 100, 60, 30), "Submit"))
    {
        SubmitMsg( messageAlert + nameField + ":  " + stringToEdit);
        SaveString();
    }
    var x: float = 270;
    var y: float = 180;
    for (var i: int;
    i < messagesTyped.length;
    i++)
    {
        y += messageSpacing;
        //print("Here");
        GUI.Label(Rect(x, y, 500, 60), messagesTyped*);*

}
}

function SubmitMsg(submittedMsg: String){
if (msgCounter == maxMessage)
{
msgCounter = 0;
}
messagesTyped[msgCounter] = submittedMsg;
print(messagesTyped[0]);
msgCounter++;
}

function SaveString(){
PlayerPrefs.SetString(“Name”,nameField);
print(“saved”);
}

But you are not using what it returns. It should be something like: var result: String; result = PlayerPrefs.GetString(nameField); Also - it does not make sence to use the second parameter (default value) when you check that the item do exist.

Another issue - you are always saving the constant "Name" - that looks like a bug. I think you have swapped the parameters. Take a look at the documentation. (Read what the parameter are called)

As I wrote basically you swapped the parameters. I have edited by answer with a full fixed code. I hope you understand the bug you made.

It works for me (when I have pressed Submit, the value is saved and next time I startup the name is replaced. Did you copy the entire source code I submitted?

It shouln't matter - the code should work. Try to create a new empty scene and add gameobject where you attach the script - just to test it.

Gotcha… It says loadedName. I really appreciate you taking the time to help me here.