- Home /
PlayerPrefs.GetInt only returns a 0
Hey im having some issues with PlayerPrefs. I recently started understanding (kinda) how it works like a week ago. For some reason everytime I launch the game bot in the editor and built version, Xtracookiesp always start with 0 as the value. I can use the reset button to make the value go back to 100 but the PlayerPrefs does not store it.
Everything works fine exept that one little thing. The script is kinda messy but I think it is readable.
var myStyle : GUIStyle;
var amStyle : GUIStyle;
var cookieStyle : GUIStyle;
var counterStyle : GUIStyle;
var PetterEmit : GameObject;
static var counter : long = 0;
var stringToEdit : String = "Type here..";
var multiplication : float = 1.2f;
var mat1:Material;
var mat2:Material;
var mat3:Material;
var mat4:Material;
var mat5:Material;
var mat6:Material;
var Xtracookies = 1;
var Xtracookiesp = 100;
var autoclickers = 0;
var autoclickersp = 50;
var petter = 0;
var pettersp = 666;
var powerminers = 0;
var powerMSP = 400;
var jcube = 0;
var jcubep = 20000;
var native_width : float = 1920;
var native_height : float = 1080;
function Awake (){
}
function Start(){
RenderSettings.skybox=mat1;
counter = PlayerPrefs.GetInt("Allcookies");
Xtracookies = PlayerPrefs.GetInt("AllXCookies");
Xtracookiesp = PlayerPrefs.GetInt("AllXCookiesp");
autoclickers = PlayerPrefs.GetInt("AllAuto");
autoclickersp = PlayerPrefs.GetInt("AllAutop");
petter = PlayerPrefs.GetInt("AllPetter");
pettersp = PlayerPrefs.GetInt("AllPettersp");
powerminers = PlayerPrefs.GetInt("AllPowerminers");
powerMSP = PlayerPrefs.GetInt("AllPowerMSP");
jcube = PlayerPrefs.GetInt("Alljcube");
jcubep = PlayerPrefs.GetInt("Alljcubep");
}
function OnGUI () {
var rx : float = Screen.width / native_width;
var ry : float = Screen.height / native_height;
GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (rx, ry, 1));
stringToEdit = GUI.TextField (Rect (1600, 990, 300, 70), stringToEdit, 100);
if (GUI.Button(new Rect(10,20,50,50), "Exit", myStyle))
{
Application.Quit();
}
GUI.Label (Rect (850, 10, 200, 50), "" + counter.ToString("n0"), counterStyle);
GUI.Label (Rect (880, 40, 200, 50), "Johannes", amStyle);
if (GUI.Button(new Rect(800,100,300,300), "", cookieStyle))
{
counter += Xtracookies;
}
if (GUI.Button(new Rect(70,20,100,50), "Reset", myStyle))
{
RenderSettings.skybox=mat1;
counter = 0;
autoclickers = 0;
autoclickersp = 50;
Xtracookies = 1;
Xtracookiesp = 100;
petter = 0;
pettersp = 666;
powerminers = 0;
powerMSP = 400;
jcube = 0;
jcubep = 20000;
PetterEmit.renderer.enabled = false;
}
if (GUI.Button(new Rect(40,180,160,50), "X-Cookies " + Xtracookiesp, myStyle) && counter > Xtracookiesp)
{
Xtracookies += 1;
counter -= Xtracookiesp;
Xtracookiesp += Xtracookiesp*multiplication;
}
GUI.Box(new Rect(200,180,40,50),"x" + Xtracookies, amStyle);
if (GUI.Button(new Rect(40,130,160,50), "Autoclick " + autoclickersp, myStyle) && counter > autoclickersp)
{
autoclickers += 1;
counter -= autoclickersp;
autoclickersp += autoclickersp*multiplication;
}
GUI.Box(new Rect(200,130,40,50),"" + autoclickers, amStyle);
if (GUI.Button(new Rect(40,230,160,50), "Petter " + pettersp, myStyle) && counter > pettersp)
{
petter += 1;
counter -= pettersp;
pettersp += pettersp*multiplication;
PetterEmit.renderer.enabled = true;
}
GUI.Box(new Rect(200,230,40,50),"" + petter, amStyle);
if (GUI.Button(new Rect(40,280,160,50), "P-Miner " + powerMSP, myStyle) && counter > powerMSP)
{
powerminers +=1;
counter -= powerMSP;
powerMSP += powerMSP*multiplication;
}
GUI.Box(new Rect(200,280,40,50),"" + powerminers, amStyle);
if (GUI.Button(new Rect(40,330,160,50), "J-Cube " + jcubep, myStyle) && counter > jcubep)
{
jcube +=1;
counter -= jcubep;
jcubep += jcubep*multiplication;
}
GUI.Box(new Rect(200,330,40,50),"" + jcube, amStyle);
}
function Update () {
PlayerPrefs.SetInt("Allcookies", counter);
PlayerPrefs.SetInt("AllXCookies", Xtracookies);
PlayerPrefs.SetInt("AllXCookiesp", Xtracookiesp);
PlayerPrefs.SetInt("AllAuto", autoclickers);
PlayerPrefs.SetInt("AllAutop", autoclickersp);
PlayerPrefs.SetInt("AllPetter", petter);
PlayerPrefs.SetInt("AllPettersp", pettersp);
PlayerPrefs.SetInt("AllPowerminers", powerminers);
PlayerPrefs.SetInt("AllPowerMSP", powerMSP);
PlayerPrefs.SetInt("Alljcube", jcube);
PlayerPrefs.SetInt("Alljcubep", jcubep);
if(stringToEdit == "Start")
{
counter+= 1000;
return;
}
if (counter > 9)
RenderSettings.skybox=mat2;
if (counter > 199)
RenderSettings.skybox=mat3;
if (counter > 499)
RenderSettings.skybox=mat4;
if (counter > 999)
RenderSettings.skybox=mat5;
if (counter > 1999)
RenderSettings.skybox=mat6;
}
InvokeRepeating("Autoclicker", 0, 0.5);
function Autoclicker() {
counter+=autoclickers;
}
InvokeRepeating("Powerminers", 0, 0.5);
function Powerminers (){
counter += powerminers*10;
}
InvokeRepeating("JCube", 0, 0.5);
function JCube () {
counter += jcube*petter*1000;
}
If anything was confusing please inform me ^^ I appriciate all comments trying to help me, thanks in advance
Answer by Aladine · Mar 13, 2014 at 08:03 PM
Do I simply replace all the SetInt with Save ins$$anonymous$$d? or should i still be using SetInt?
when using SetInt, you are righting in the playerPrefs variable, which mean all your data will be lost when you close the game, by using PlayerPrefs.Save, you are saving all the data saved in the setInt to the Disk, so nothing will be lost, I$$anonymous$$PORTANT like the docs said, do not use PlayerPrefs.Save in the update function, check OnApplicationQuit()
Oh, but so far all my variables has been saved by using SetInt, if i close the game and reopen it all my progress is still there, so nothing was lost. Exept, the Xtracookiesp, this is the only variable that wont work properly.
Im going to test PlayerPrefs.Save now. But in the docs it said it could be used as an "checkpoint" maker, in case the game crash.
So could I use for example InvokeRepeat("Save", 0, 40.0); function Save () { PlayerPrefs.Save(); {
could it be because your are multiplying an int by a float (multiplication) ?? please make a debug.log before your SetInt.
Aladine, you were right again, haha. I made the multiplication variable to an int and it seems to be working without the new save function. Thanks for all the suppport guys ^^