PlayerPrefs for saving strings (453220)

I’m trying out the PlayerPrefs Class with my code for a bulletin board now and I must be doing something wrong. I’m trying to load previously saved strings with OnLevelWasLoaded and then save new strings when the submit button is pressed. I can’t seem to get it to work. I’d really appreciate it if someone could take a look at this.

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];
   
}

function OnLevelWasLoaded(){
    //get input
    loadLevel();
}

function loadLevel(){
    if(PlayerPrefs.HasKey(nameField+ stringToEdit)){
    Application.LoadLevel(PlayerPrefs.GetString(nameField+ stringToEdit));
    }   
}

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);
        saveMessage();
        
    }
    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[i]);
    }
    }
function SubmitMsg(submittedMsg: String)
{
    if (msgCounter == maxMessage)
    {
        msgCounter = 0;
    }
    messagesTyped[msgCounter] = submittedMsg;
    print(messagesTyped[0]);
    msgCounter++;
}

function saveMessage(){
PlayerPrefs.SetString(nameField, stringToEdit);
print("saved");
}

You’re using “GetString(nameField+ stringToEdit)”, but “PlayerPrefs.SetString(nameField, stringToEdit)”, which are two very different things.

–Eric

How is it supposed to be? I thought you were supposed to ‘Get’ to load and ‘Set’ to save. Or have I misunderstood you?

Other than being in the wrong forum…

It’s time to play spot the differance!

I’ve done the reading…I just don’t understand it. Nevermind, I’ll take my questions elsewhere.

to set a string you need to give a reference first and then what you want to save as second param.
to read a string you simply read the reference and you have your saved param.

You’re using “nameField+ stringToEdit” as the key with GetString, but “nameField” as the key with SetString.

–Eric