Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Cooper37 · Sep 08, 2013 at 12:38 AM · playerprefssavingupgradecoinshealth

Help Saving Max Health With PlayerPrefs

I've been working on a simple upgrade system where the player has to collect 100 coins, and every time a hundred is collected, the player's health is upgraded. I have tried many, many ways to save the maxHealth var using playerprefs. Maybe someone has an answer for me, that would be much appreciated.

Here is some snippets of the Health script:

     var curHealth = 3;
     static var coins = 0;
     static var maxHealth = 3;
     coins = PlayerPrefs.GetInt("Coin");
     
     function Start(){
          curHealth = maxHealth;
     }
     
     function FixedUpdate(){
          if(curHealth <= 0){
             Application.LoadLevel(Application.loadedLevel);
         }
         if(coins == 100){
             maxHealth ++;
             curHealth = maxHealth;
             coins = 0;
         }
         if(maxHealth > 10){
             maxHealth = 10;
             curHealth = maxHealth;
         }
     }
      
     function OnTriggerEnter(other : Collider){
         if(other.gameObject.tag == "Health"){
             Destroy(other.gameObject);
             curHealth = maxHealth;
         }
         if(other.gameObject.tag == "Coin"){
             coins ++;
             Destroy(other.gameObject);
             PlayerPrefs.SetInt("Coin",coins);         
         }
     }
 
 
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
1
Best Answer

Answer by Garrafote · Sep 08, 2013 at 01:24 AM

I'm not used to javascript but this is what i would do:

 #pragma strict
 
 var curHealth : int;
 var coins : int;
 var maxHealth : int;
 
 function Awake() {
     // setup playerprefs vars on awake
     coins = PlayerPrefs.GetInt("Coin", 0);
     maxHealth = PlayerPrefs.GetInt("MaxHealth", 3);
 }
 
 function Start(){
      curHealth = maxHealth;
 }
 
 function FixedUpdate(){
     if(curHealth <= 0){
         Application.LoadLevel(Application.loadedLevel);
     }
     if(coins == 100){
         maxHealth ++;
         curHealth = maxHealth;
         coins = 0;

         // update playerpref vars every time the value change
         PlayerPrefs.SetInt("Coin",coins);
         PlayerPrefs.SetInt("MaxHealth",maxHealth);
     }
     if(maxHealth > 10){
         maxHealth = 10;
         curHealth = maxHealth;

         // update playerpref vars every time the value change
         PlayerPrefs.SetInt("MaxHealth",maxHealth);
     }
 }
 
 function OnTriggerEnter(other : Collider){
     if(other.gameObject.tag == "Health"){
         Destroy(other.gameObject);
         curHealth = maxHealth;
     }
     if(other.gameObject.tag == "Coin"){
         coins ++;
         Destroy(other.gameObject);

         // update playerpref vars every time the value change
         PlayerPrefs.SetInt("Coin",coins);         
     }
 }

Is that wat you want?

Comment
Add comment · Show 5 · 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
avatar image Cooper37 · Sep 08, 2013 at 01:39 AM 0
Share

Well I'll be damned. It's exactly what I needed. I've been working on this for weeks and the answer was that simple. Well, thank you. There was some code that's really not needed like line 27 and the coins pref outside the functions. And hey, you even set up my maxHealth limit like I was!

This is exactly what I needed. Thanks!

avatar image Cooper37 · Sep 08, 2013 at 01:57 AM 0
Share

On second thought, there's a little problem. When I reload the level (Application.LoadLevel) or when I go to the main menu and press continue, the maxHealth increments by one. I don't want that. Where's the problem there?

avatar image Cooper37 · Sep 08, 2013 at 02:12 AM 0
Share

Never$$anonymous$$d the last post. I guess those lines were needed.

Thank you, you're a genius.

avatar image Garrafote · Sep 08, 2013 at 05:32 PM 0
Share

Im glad I could help you @Cooper37!

Can you please help the community by accepting the answer?

avatar image wasicool7 Garrafote · Aug 03, 2016 at 11:39 PM 0
Share

Hey is there a way you can convert this to c# please and thank you! :)

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

17 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

Related Questions

Need help with using Player Prefs to save number of coins collected. 1 Answer

PlayerPrefs resetting values on startup/restart? 1 Answer

Detect from coins 0 Answers

Coin System with PlayerPrefs 1 Answer

Saving and load from player prefs in Unity3D on mobile devices?? 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