- Home /
The question is answered
PlayerPrefs Question
I'm trying to build a message box and am using PlayerPrefs to save new messages and reload them after the app is closed and reopened. However i've ended up having the namefield load up rather than the message. I've taken a closer look and can see why this has happened and it seems I need to change the variable from nameField to something like messagesTyped.
The problem is the messagesTyped variable I currently have won't work with the SetString and GetString commands. I was wondering if anyone has any suggestions for how I can make this work. Should I create a new variable to signal the messages submitted with the submit button and use that with the PlayerPrefs?
Here's the code as it is at the moment:
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"+nameField);
}
}
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"+nameField);
}
These are beco$$anonymous$$g a set of "please write my script for me" questions. Do you actually understand what is already in that script? It seems that you have found something somewhere and are trying to hack it to serve some other purpose, rather than learning how it works and then modifying it to your needs.
You should at least understand enough to write your question, which I'm just guessing is: "How do I store an array of String in PlayerPrefs?", which you can find an answer for by just typing "playerprefs array string" into the search box above.
When you do not understand "your own" code, you can't ask answerable questions, so you end up with "Here is a script, please fix it", which is just junk on this site, since it will never be useful to anyone but yourself.
Erm...okay. Either way you answered my question. So thanks anyway angry person
It's a tough job trying to $$anonymous$$ch fishing to people that just want a fish, already!
Enjoyable metaphors aside. I can see code anywhere (especially on the wiki page) but if you're very new to this and trying to learn, none of it is substantially is explained.
I have written quite a lot of this code above but yes some people have helped me out immensely along the way and I have learned a great deal from them. However arrays remain a sticking point, remember this aint easy for everyone.
Aaaanyway, off back to the grind. Cheers anyway
I'm just grumpy from the Influenza I'm suffering from at the moment. I hate Winter.
Follow this Question
Related Questions
PlayerPrefs for saving strings 1 Answer
PlayerPrefs script problem 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Storing in PlayerPref based on timer 1 Answer
Saving a button in PlayerPrefs 2 Answers