Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
        }
    }
}
share|improve this question
1  
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) –  Assorted Trailmix yesterday
1  
@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. –  Lohoris yesterday
    
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? –  jhocking yesterday
    
@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. –  Assorted Trailmix 12 hours ago
    
@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. –  Assorted Trailmix 12 hours ago

2 Answers 2

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");
share|improve this answer

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.

share|improve this answer
    
It's most likely the lack of Save() being called. –  Byte56 yesterday
    
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. –  Lohoris yesterday
    
@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. –  Byte56 yesterday
    
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. –  McAden yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.