x


PlayerPrefs for saving strings

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");
}
more ▼

asked Aug 17 '11 at 10:28 AM

blacksuit_films gravatar image

blacksuit_films
1 7 7 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

You're looking up a sum of two string when you check HasKey, but then you use a different parameter when you call GetString. That's why it's not working.

more ▼

answered Aug 17 '11 at 12:40 PM

Waz gravatar image

Waz
6.4k 22 33 70

Thanks for the response. I've since changed that line to read:

if(PlayerPrefs.HasKey(nameField)){ Application.LoadLevel(PlayerPrefs.GetString(nameField, stringToEdit));

but now i'm getting an error message saying:

Level 'What's The News?' (-1) couldn't be loaded because it has not been added to the build settings. To add a level to the build settings use the menu File->Build Settings...

I havent heard anything about adding PlayerPrefs to build settings...or am I not supposed to call 'Application.LoadLevel' as part of loading these strings?

Aug 17 '11 at 12:53 PM blacksuit_films

You're calling LoadLevel with that string, hence you get that result. I don't know what you expected to happen.

Aug 17 '11 at 12:57 PM Waz

Don't add Application.LoadLevel unless you are trying to load a scene. If so, just add it to the build settings.

Oct 11 '12 at 08:07 PM ChromeGames
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3669
x435
x418
x297
x294

asked: Aug 17 '11 at 10:28 AM

Seen: 957 times

Last Updated: Oct 11 '12 at 08:15 PM