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*);*
}
}
function SubmitMsg(submittedMsg: String)
{
if (msgCounter == maxMessage)
{
msgCounter = 0;
}
messagesTyped[msgCounter] = submittedMsg;
print(messagesTyped[0]);
msgCounter++;
}
function saveMessage(){
PlayerPrefs.SetString(nameField, stringToEdit);
print(“saved”);
}