Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ForeignGod · Mar 13, 2014 at 07:25 PM · javascriptguiplayerprefsvalues

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Aladine · Mar 13, 2014 at 08:03 PM

PlayerPrefs.Save

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ForeignGod · Mar 13, 2014 at 08:11 PM 0
Share

Do I simply replace all the SetInt with Save ins$$anonymous$$d? or should i still be using SetInt?

avatar image Aladine · Mar 13, 2014 at 08:16 PM 1
Share

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()

avatar image ForeignGod · Mar 13, 2014 at 08:22 PM 0
Share

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(); {

avatar image Aladine · Mar 13, 2014 at 08:30 PM 1
Share

could it be because your are multiplying an int by a float (multiplication) ?? please make a debug.log before your SetInt.

avatar image ForeignGod · Mar 13, 2014 at 08:38 PM 1
Share

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 ^^

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Lowering a variable 1 number lowering it entirely? 1 Answer

How do to you save time for each player and display it? 2 Answers

Whats the best way to save an enum state from scene to scene??? 1 Answer

Issue with PlayerPrefs and local highscore 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges