- Home /
saving numbers with PlayerPrefs
So im testing some app making in unity (I know unity isnt for apps). The app is very simple. There is 4 smileys. A mad smiley, a "okay" smiley, a "good smiley, and a "very good" smiley. Its like judgemnet app. And when you press on of the smileys, it gives 1 point to that smiley. And then you can see the statistics like this:
Mad: 8 Okay: 3 Good: 20 Very good: 34
But im having problems with saving the numbers. I have done it before with highscores, but its a little different. I know i need to use PlayerPrefs but i dont know how to use it in this case. What i want is that there is a "saved number". And when i press the smiley the "saved number" gets 1+. And i then can go out of the app and open it again, and its still 1.
Here is my code: #pragma strict
var rigtiggodKlik = 0; //How many voted on this smiely.
function Start () {
}
function Update ()
{
}
function OnMouseDown () {
rigtiggodKlik++;
Application.LoadLevel("Takforstemme"); //Loading a new level that says: "Thanks for voting!".
}
So i want the "rigtiggodKlik" to be saved.
Answer by HarshadK · Jun 16, 2014 at 01:18 PM
Psuedo-code:
function OnMouseDown () {
// Get value of your rigtiggodKlik from your Playerprefs using Playerprefs.GetInt("rigtiggodKlik");
// Add one and set value of rigtiggodKlik using Playerprefs using Playerprefs.SetInt("rigtiggodKlik", rigtiggodKlik++);
// Save your Playerprefs using Playerprefs.save
Application.LoadLevel("Takforstemme"); //Loading a new level that says: "Thanks for voting!".
}
EDIT: Updated to reflect the updated code derived from the code of OP.
var rigtiggodKlik : int = 0; //How many voted on this smiely.
var rigtigGod : int = PlayerPrefs.GetInt("rigtiggodKlik", 0);
function Start () {
}
function Update ()
{
}
function OnMouseDown () {
Debug.Log("MouseDown");
rigtiggodKlik = PlayerPrefs.GetInt("rigtiggodKlik");
PlayerPrefs.SetInt("rigtiggodKlik", rigtiggodKlik++);
PlayerPrefs.Save();
Application.LoadLevel("Takforstemme"); //Loading a new level that says: "Thanks for voting!".
}
So according to what i saw in the code, i got it to this: #pragma strict
var rigtiggod$$anonymous$$lik = 0; //How many voted on this smiely.
var rigtigGod : int = PlayerPrefs.GetInt("rigtiggod$$anonymous$$lik", 0);
function Start () {
}
function Update ()
{
}
function On$$anonymous$$ouseDown () {
Debug.Log("$$anonymous$$ouseDown");
PlayerPrefs.GetInt("rigtiggod$$anonymous$$lik");
PlayerPrefs.SetInt("rigtiggod$$anonymous$$lik", rigtiggod$$anonymous$$lik++);
PlayerPrefs.Save();
Application.LoadLevel("Takforstemme"); //Loading a new level that says: "Thanks for voting!".
}
But it does not work.
PlayerPrefs.GetInt("rigtiggod$$anonymous$$lik");
//This just gets the value out of the playerprefs, you need to stuff it into a variable:
rigtiggod$$anonymous$$lik = PlayerPrefs.GetInt("rigtiggod$$anonymous$$lik");
@Landern that's quite a shame for me. How did I miss that?
@Harshad$$anonymous$$, probably because you're trying to answer all the questions :)
Answer by Landern · Jun 16, 2014 at 01:18 PM
PlayerPrefs.SetInt("WhatEverYouWantToCallItInternally", rigtiggodKlik);
PlayerPrefs.Save();
then when you load
var rigtiggodKlik:int = PlayerPrefs.GetInt("WhatEverYouWantToCallItInternally", 0); // the zero here is the default if the key isn't found.