- Home /
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[i]);
}
}
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?
It's okay I' know that there is a 1$$anonymous$$B limit. I have had no results as yet with this code. So far I have only used 4$$anonymous$$B of that space
Answer by Mortennobel · Aug 17, 2011 at 07:57 PM
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[i]);
}
}
function SubmitMsg(submittedMsg: String){
if (msgCounter == maxMessage)
{
msgCounter = 0;
}
messagesTyped[msgCounter] = submittedMsg;
print(messagesTyped[0]);
msgCounter++;
}
function SaveString(){
PlayerPrefs.SetString("Name",nameField);
print("saved");
}
GetString is currently in my code already within function Get$$anonymous$$essages.
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)
Okay, I sortof see what you mean. I think my main misunderstanding here is the var and string at the beginning of your proposed code. what would I put in as the result for the var and string?
Apologies but filling in the blanks isn't something I'm good at.
I don't understand the documentation theres not enough info for a newbie. The only way for me to learn this properly is to see it done but there are no tutorials.
Answer by blacksuit_films · Aug 17, 2011 at 08:46 PM
Gotcha... It says loadedName. I really appreciate you taking the time to help me here.
Your answer
Follow this Question
Related Questions
how to save a load a game? 1 Answer
PlayerPrefs Question 0 Answers
Save game support, how do I load audio in a sertain time frame? 0 Answers
How do I save and load the state of the GameObjects with PlayerPrefs? 0 Answers
There are wrong mathematical equations in PlayerPrefs. How can I resolve it? 1 Answer