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 Hydraxia · Jul 17, 2013 at 05:56 PM · c#playerprefssavemoneystats

Need help with PlayerPrefs

Hello! Im using c#, and i have this script. Note, that this is not the full script. I deleted all others, so it's not so messy and you can read it easier ( I know, i have bad english knowledge ).

There are 2 scenes: MainMenu, and the Map. This is the script in the Map, named PlayerControl. ( It will give you money by picking up but now i coded by pressing ''A'' key. ) When i leave ( escape button ) it goes to the menu. When i go back ( There is a button that drops the player to ''Map'' ) it doesn't saves the players ''money''.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControl : MonoBehaviour {
     
         public int money;
     
     // Use this for initialization
     void Start () {
         PlayerPrefs.GetInt("money", money);
     }
     
     
     // Update is called once per frame
     void Update () {
         
          if (Input.GetKeyDown(KeyCode.Escape))     {    
                     PlayerPrefs.Save();
             Application.LoadLevel("Menu");
         }
          if (Input.GetKeyDown(KeyCode.A)) {
                     money = money + 1;
             PlayerPrefs.SetInt("Money", money);
         }
     }
     void OnGUI () {
         
         GUI.Label (new Rect (50, 75, 100, 30), "Money");
         GUI.Label (new Rect (150, 75, 100, 30), money.ToString());
     }
 }




Thank you!

( Here is the full script, but im almost 100% sure that these are not required for saving )

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControl : MonoBehaviour {
     
         public    float accelerationofallstuff = 1;
         public static float nanometersinglerun = 0;
         public int money;
     
     // Use this for initialization
     void Start () {
         Screen.showCursor = false;    
         bool showrewardsondeath = false;
         PlayerPrefs.GetInt("money", money);
     }
     
     
     // Update is called once per frame
     void Update () {
         
         accelerationofallstuff = Time.timeSinceLevelLoad / 20;
         nanometersinglerun = Time.timeSinceLevelLoad * accelerationofallstuff;
         
            bool showrewardsondeath = false;
         
            float deltax = Input.GetAxis("Mouse X");
            transform.position = new Vector3 (transform.position.x + deltax, 0, 0);
         
         if (transform.position.x > 10)
            transform.position = new Vector3 (10,0,0);
         if (transform.position.x < -10)
             transform.position = new Vector3 (-10,0,0);
         
          if (Input.GetKeyDown(KeyCode.Escape))     {    
                     PlayerPrefs.Save();
             Application.LoadLevel("Menu");
         }
          if (Input.GetKeyDown(KeyCode.A)) {
                     money = money + 1;
             PlayerPrefs.SetInt("Money", money);
         }
     }
     void OnGUI () {
         
         GUI.Label (new Rect (50, 25, 100, 30), "Nanometer");
         GUI.Label (new Rect (50, 50, 100, 30), "Time");
         GUI.Label (new Rect (50, 75, 100, 30), "Money");
         GUI.Label (new Rect (150, 25, 100, 30), nanometersinglerun.ToString("F1"));
         GUI.Label (new Rect (150, 50, 100, 30), Time.timeSinceLevelLoad.ToString("F1"));
         GUI.Label (new Rect (150, 75, 100, 30), money.ToString());
         
         if (Time.timeSinceLevelLoad < 2)
             GUI.Label (new Rect (Screen.width /2 -50, 50, 160, 30), "Activating Nanobot");
         
         if (Time.timeSinceLevelLoad > 3)
             GUI.Label (new Rect (Screen.width /2 -30, 50, 100, 30), "Charging...");
     }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Slobdell · Jul 17, 2013 at 11:15 PM

This doesn't give you a compile error?! In your start method playerprefs.getInt has two parameters. Should be PlayerPrefs.GetInt("money")

Comment
Add comment · Show 3 · 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 Hydraxia · Jul 18, 2013 at 03:53 AM 0
Share

Not, i have only 1 warning (not that red), but that's only because the variable ''showrewardsondeath'' is never used. Everything works, except the saving. I can see the money raising when tapping ''A'' button. When i press ''Escape'', it goes to the $$anonymous$$ain$$anonymous$$enu. There is the button ''Launch'',and when i start again, i don't have the ''money''.

avatar image Slobdell · Jul 18, 2013 at 04:06 AM 1
Share

Read my answer again...

avatar image Hydraxia · Jul 18, 2013 at 09:53 AM 0
Share

Sorry, but it doesn't work. It lets me to write: PlayerPrefs.GetInt("money") and PlayerPrefs.GetInt("money", money) too.

avatar image
1

Answer by hatuf · Jul 18, 2013 at 10:55 AM

 PlayerPrefs.SetInt("Money", money);
 PlayerPrefs.GetInt("money", money);



The keys are case-sensitive.

Comment
Add comment · Show 3 · 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 Hydraxia · Jul 18, 2013 at 01:00 PM 0
Share

Thanks, i didn't knew that,but it still doesn't work.

avatar image hatuf · Jul 18, 2013 at 01:08 PM 1
Share

Ok, I see another problem:

 void Start () {
        PlayerPrefs.GetInt("money", money);
 }

GetInt returns the money but it's not being assigned to anything. Change it to:

 money = PlayerPrefs.GetInt("money");
avatar image Hydraxia · Jul 18, 2013 at 06:42 PM 0
Share

YES! Finally it works! Thank you!

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

17 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

Related Questions

How do I save a custom class of variables to playerprefs? 2 Answers

C# Issues with either PlayerPrefs.SetVariable or UnityEvent.Invoke 1 Answer

Unable to get save score between levels working 1 Answer

How to create log by PlayerPrefs? 1 Answer

Problem with High Score System using PlayerPrefs [C#] 1 Answer


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