Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

Saving data with files 1 Answer

Problem saving with PlayerPrefs 0 Answers

PlayerPrefs on Mobile 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges