• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Panthers132 · Dec 16, 2015 at 03:47 AM · playerprefsloadingsavingcoin

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;
         }
     }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

36 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Playerprefs don't work on Android 0 Answers

Saving a Player Pref Exclusive to that instance of a script 0 Answers

PlayerPrefs help ! 1 Answer

What is the best way for loading ,editing and saving Levels in unity ? 1 Answer

PlayerPrefs working on PC but not on Android. [2020] 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges