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.
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!".
}
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.