- Home /
coins go to 0
I want to be able to go to a shop scene from the main scene and have my coins the same in both of them but when i try to go to the shop scene the coins just turn to 0. Anyone know what the problem is? Heres my scripts:
Coins Script
public int coin = 0;
bool hit = false;
public GUISkin myskin;
void Start(){
coin = PlayerPrefs.GetInt ("Coins",0);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "coin")
{
//coin ++;
audio.Play();
hit = true;
}
}
void Update(){
if (hit) {
coin ++;
hit = false;
}
}
void OnDisable()
{
PlayerPrefs.SetInt ("Coins", (int)coin);
PlayerPrefs.Save ();
}
void OnGUI()
{
GUI.skin = myskin;
GUI.Box(new Rect(100,10,80,80), "");
GUI.Label(new Rect(200,10,200,100), "" + coin);
}
Shop Script
public GUISkin myskin;
int coin;
int touch = 0;
int time = 3;
void start(){
coin = PlayerPrefs.GetInt ("Coins",0);
}
void OnGUI(){
GUI.skin = myskin;
GUI.Label (new Rect (500,10,200,200), "Coins: " + (int)coin);
if (GUI.Button (new Rect (Screen.width / 2 + 10, Screen.height / 2 + 2 * Screen.height / 5 + 8, Screen.width / 1 - 5, Screen.height / 3), "main")) {
Application.LoadLevel ("main");
}
if (GUI.Button (new Rect (100, 40, 100, 100), "shield")) {
time = 5;
coin += 100;
}
if (GUI.Button (new Rect (Screen.width / 4 + 10, Screen.height / 4 + 2 * Screen.height / 20, Screen.width / 2 - 10, Screen.height / 5), "Score x3")) {
coin = coin - 10;
}
}
void OnDisable(){
PlayerPrefs.SetInt ("Coins",(int)coin);
PlayerPrefs.SetInt ("time", time);
PlayerPrefs.Save();
}
I would suggest using DontDestroyOnLoad. Yet from what I see you're not saving to PlayerPrefs before loading the scene. $$anonymous$$y suggestion would be to have one script that's constantly auto saving all your PlayerPrefs data. Unless you want to save manually.
Sorry, I'm new to unity. How could I make the script that could constantly auto save my PlayerPrefs data?
Answer by Kiwasi · Apr 20, 2015 at 12:43 AM
You've got two scripts independently loading, updating, and saving the same variable to player prefs. This is bad practice, and probably why the value in player prefs keeps getting reset.
You'll want to load the value in just one of your scripts, and access it from the other script.
Agreed. What I've done in the past is create a "SaveData" script (or whatever) that is static, and create some functions on it that wrap the Player Prefs functionality that you want (basically identical functions that just call the player prefs function).
This is also useful because if you decide to save your data in another way later, besides player prefs (like using a cloud save system), then you've successfully already abstracted out the saving process, and will only have to change that script in one place.
could you show me how to make this script because i am fairly new to unity and dont understand how to make the saving process
Also could you tell me how i could save the coins so that the same number of coins would show if i turn the game off and if i was using the static functions?
Your answer
![](https://koobas.hobune.stream/wayback/20220613193529im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Shop system isn’t working and I don’t know why 1 Answer
Bullet bounce issue using Vector3.reflect 1 Answer
How to change button text in simple character shop ? 1 Answer
Endless Runner Coin Magnet Not working. 0 Answers
Problem with SUM 1 Answer