- Home /
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");
}
Answer by Waz · Aug 17, 2011 at 12:40 PM
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.
Thanks for the response. I've since changed that line to read:
if(PlayerPrefs.Has$$anonymous$$ey(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?
You're calling LoadLevel with that string, hence you get that result. I don't know what you expected to happen.
Don't add Application.LoadLevel unless you are trying to load a scene. If so, just add it to the build settings.
Your answer
Follow this Question
Related Questions
PlayerPrefs Question 0 Answers
reference variables within my xml file. 0 Answers
Loading a save/playerprefs from a different game? 2 Answers
PlayerPrefs Int to Float 1 Answer
Loading error when using PlayerPrefs.GetInt with WebPlayer 2 Answers