- Home /
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...");
}
}
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")
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''.
Sorry, but it doesn't work. It lets me to write: PlayerPrefs.GetInt("money") and PlayerPrefs.GetInt("money", money) too.
Answer by hatuf · Jul 18, 2013 at 10:55 AM
PlayerPrefs.SetInt("Money", money);
PlayerPrefs.GetInt("money", money);
The keys are case-sensitive.
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");
Your answer
Follow this Question
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