2
\$\begingroup\$

I am stuck on how to save the player's amount of money. When I build the game in the exe as soon as I shutdown the game my money value gets lost. I want this money to be saved. I am using the following code in UnityEngine:

using UnityEngine.UI; using System.Collections;

namespace CompleteProject
{
    public class MoneyManager : MonoBehaviour {
        public static int money; // The player's money.
        Text moneytext;          // Reference to the Text component.

        void Awake ()
        {
            // Set up the reference.
            moneytext = GetComponent <Text> ();
            money=print(PlayerPrefs.GetInt("Money"));
        }

        void Update ()
        {
            moneytext.text = "" + money;
            PlayerPrefs.SetInt ("Money", money);
        }
    }
}
\$\endgroup\$
7
  • 1
    \$\begingroup\$ Be weary of using PlayerPrefs for storing game data. It may or may not matter to you, but PlayerPrefs is extremely easy to access and modify. There's no fool-proof method to protect stored game data, but PlayerPrefs is on the extremely lax side of protection schemes (and definitely don't use it to store anything like passwords) \$\endgroup\$ Mar 23, 2015 at 17:38
  • 1
    \$\begingroup\$ @AssortedTrailmix I disagree: 1. as long as the game is offline, who cares if someone cheats? 2. if the game is online, nothing should be authoritative client-side anyway. \$\endgroup\$
    – o0'.
    Mar 23, 2015 at 20:37
  • \$\begingroup\$ Depends which password you mean. If you mean the player's password, why not? If you mean any other password, why does the player's machine even know that? \$\endgroup\$
    – jhocking
    Mar 23, 2015 at 21:42
  • \$\begingroup\$ @Lohoris I chose my words carefully: "It may or may not matter to you" (some people do care) and "There's no fool-proof method to protect stored game data, but PlayerPrefs is on the extremely lax side of protection schemes". I'd say you can put a tiny bit more effort and do a lot better than PlayerPrefs for things like that, especially in games with things like High Score boards (which wouldn't necessitate client-side auth). It's still not impervious, but it's not as simple as casually opening the registry and doing a search. \$\endgroup\$ Mar 24, 2015 at 12:14
  • \$\begingroup\$ @jhocking people tend use the same password just about everywhere. I wouldn't want to store their passwords in plain-text in an easily accessible area. \$\endgroup\$ Mar 24, 2015 at 12:17

2 Answers 2

8
\$\begingroup\$

You're assigning the value of money to the print return value, not the actual int value being returned.

money=print(PlayerPrefs.GetInt("Money"));

Should be

money=PlayerPrefs.GetInt("Money");
\$\endgroup\$
0
\$\begingroup\$

It may be elseswhere but as far as we can see from the code you've provided there doesn't seem to be a call to PlayerPrefs.Save(); and without it the value won't be persisted.

EDIT: Per comment from @Lohoris this isn't the case as it saves automatically on Application.Quit. As @Byte56 points out the issue may still be related if the application is being terminated via the Task Manager.

\$\endgroup\$
4
  • \$\begingroup\$ It's most likely the lack of Save() being called. \$\endgroup\$
    – House
    Mar 23, 2015 at 18:29
  • \$\begingroup\$ Wrong: Writes all modified preferences to disk. By default Unity writes preferences to disk on Application Quit. In case when the game crashes or otherwise prematuraly exits, you might want to write the PlayerPrefs at sensible 'checkpoints' in your game. This function will write to disk potentially causing a small hiccup, therefore it is not recommended to call during actual gameplay. \$\endgroup\$
    – o0'.
    Mar 23, 2015 at 20:36
  • \$\begingroup\$ @Lohoris Perhaps OP is killing the executable with task manager? If they haven't implemented some kind of exit, it's possible this is causing the player prefs to not be saved. \$\endgroup\$
    – House
    Mar 23, 2015 at 20:48
  • \$\begingroup\$ Hmm...hadn't read that. When I got familiar with PlayerPrefs I just assumed I needed to call Save and always have. After having that pointed out I thought it might be an issue with the editor but I did a test (4.5.2) and it did indeed save between runs without manually calling Save. \$\endgroup\$
    – McAden
    Mar 23, 2015 at 21:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .