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");
}
Damn this is tricky. I just tried your suggestion and now i get an error message.
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… UnityEngine.Application:LoadLevel(String) Status Field:loadLevel() (at Assets/scripts/Status Field.js:26) Status Field:OnLevelWasLoaded() (at Assets/scripts/Status Field.js:21)
I havent heard anything about adding PlayerPrefs to build settings…or am I not supposed to call ‘Application.LoadLevel’ as part of loading in these strings?
First, you’re storing and later retrieving a string from PlayerPrefs. You do that if you want to remember it even when the app is terminated. Seems you got that working now.
Second, you’re loading levels (scenes). You need to explicitly include the scenes you need in the project, otherwise they won’t be exported. You apparently haven’t added the “What’s The News?” scene yet. You include them via File / Build Settings, like the error message tells you.
I was confusing the Application.LoadLevel call with the Player prefs load() function. “whats the news” isn’t a level its a string, but I was mistakenly asking the program to load the string as a level…doh!
Now I’ve got the code without any errors and am getting ‘save’ and ‘load’ printed at the right moments…but the problem remains. After the app is terminated the strings don’t come back though it says thy’ve been loaded!
cheers but the support documentation was the first place I looked for assistance. I only use the forums when I need things explained properly.
You’d think that out of all of the thousands of posts there would be one decent example of this script in action.
This doesn’t seem to be working either. I’m trying to make it so that the nameFIeld and stringToEdit remain after the app has been closed and reopened. So essentially the both strings have to be saved and reloaded. Not sure why I can’t see anything happening even though it is printing ‘saved’ and ‘loaded’ and I have no error messages.
Okay, sorry about that. I’m trying this now. I’m creating a save function with PlayerPrefs.Save so I can call it from within the OnGUI but unity says ‘Save’ is not a member of Unity Engine.PlayerPrefs…erm really?
if the scene name is saved into playerprefs ( the name like it’s in the build settings) then you simply load a the scene with :
Application.LoadLevel(PlayerPrefs.GetString (“myReference”));
Okay, can you give me an example of how to write the ‘PlayerPrefs.Save’ function because this is all just trial and error here. No matter what I try Unity just doesn’t recognise the function.
tried writing: function PlayerPrefs.save()
but I still get the error from earlier.
The loading makes more sense though, cheers for that.
Okay PlayerPrefs.Save(); wont work in javascript and I can’t find any reference to an alternative.Back to square one now trying to make ‘SetString’ and ‘GetString’ work. Cheers for trying.