How to allow coins to increase its Player.Prefs file and save, but then load it again next time.
Hey guys, so my title really explains it. I am currently creating a three lane highway style game that has objects, obstacles and enemies flying towards the player. I currently have a coin system in place, where each coin the player collects adds one to the 'coins' total. However, the issue i cant seem to crack is the ability to save the coin total somewhere, and then load it the next time the user plays the game. Here is the coin manager script i have: using UnityEngine; using System.Collections; using UnityEngine.UI;
public class CoinManager : MonoBehaviour {
public static int coinTotal;
public bool gameOver;
public Text coinText;
void Awake(){
coinTotal = PlayerPrefs.GetInt ("Coins");
gameOver = GameObject.Find ("Canvas").GetComponent<GameOverManager>().gameOver;
}
void Update () {
coinText.text = "Coins: " + coinTotal;
if(gameOver == true){
PlayerPrefs.SetInt("Coins", coinTotal);
}
}
}
Now currently it finds the Canvas, where my gameover manager script is located. On this script i have a trigger called gameOver. And i try to use that here. I am trying to get the best performance out of my game as it has been designed for mobile. So in terms of the issue i have, within the game itself, like when the first level is being played, the player can collect coins and see the total displayed on the screen. However, when they die and restart, or quit and come back, the total resets.
Any help would be greatly appreciated, and if you need any more clarification don't hesitate to ask. I appreciate any help.
Here is my GameOverManager script if you guys were interested. `
using UnityEngine;
using System.Collections;
public class GameOverManager : MonoBehaviour {
public Bullet isDead;
public Ammunition isOutOfAmmo;
public bool gameOver;
Animator anim;
void Awake(){
anim = GetComponent<Animator> ();
gameOver = false;
}
void Update(){
if(Bullet.isDead == true){
anim.SetTrigger("GameOver");
gameOver = true;
}
if(Ammunition.isOutOfAmmo == true){
anim.SetTrigger("GameOver");
gameOver = true;
}
Bullet.isDead = false;
}
}
Answer by SandvichPL123 · Mar 28, 2016 at 08:02 AM
Hello! Here's how to fix it: First off i'd try changing the line that displays the text to "coins:" + coinsTotal.toString(); Because text displays string type not int type. Your main issue probably lies in displaying the coin amount, as to me it seems that it should work. Check in inspector if the coinTotal amount is getting saved and post me back.
Your answer
Follow this Question
Related Questions
Playerprefs don't work on Android 0 Answers
Saving a Player Pref Exclusive to that instance of a script 0 Answers
Saving data with files 1 Answer
Problem saving with PlayerPrefs 0 Answers
PlayerPrefs on Mobile 0 Answers