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 FH_88 · Mar 20, 2014 at 04:19 PM · javascriptsystemenergyregeneration

Heart/Energy system

I have been looking around, trying to find a way to make a "heart" system. Where you have got 5 hearts, that is showed on the "menu scene" and then if you lose a level you lose one heart, and then every 30 minute a heart regenerates. Then if you have zero hearts you cant play before they regenerate, but if you have a heart you are able to play. How would I do this?

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

Answer by supernat · Mar 20, 2014 at 04:35 PM

Here's the basic gist for one way to do it:

 On startup:
   Load the last "heart time" from PlayerPrefs (this would be a time describing when the last heart was gained)
   Get current time
   Set next heart time to last heart time + 30 minutes
   While (current time is > next heart time)
     Add 1 to hearts available
     next heart time = next heart time + 30 minutes
     save current time to PlayerPrefs as last heart time.

 In Update:
   Check if current time > next heart time
   If so:
     Add 1 to hearts available
     save current time to PlayerPrefs as last heart time.

 In your OnGUI:
   Loop through all hearts showing
   If (index of current heart < hearts available)
     draw full red heart
   else
     draw empty gray heart
Comment
Add comment · Show 2 · 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 FH_88 · Mar 20, 2014 at 05:43 PM 1
Share

Thanks for your respond.

I understand most of the things you mention, but how would i make the Load the last "heart time" from PlayerPrefs.

avatar image supernat · Mar 20, 2014 at 07:22 PM 1
Share

PlayerPrefs is a class available in the script. For instance, in C#, you could do this:

   int lastTime = PlayerPrefs.getInt("LastHeartIncreaseTime");
   if (lastTime == 0)
   {
     Debug.Log("Last time doesnt exist ");
     lastTime = // your function here to compute time, could be UTC since 1970 in seconds, ms, could store a date, doesn't have to be an integer.
     PlayerPrefs.SetInt("LastHeartIncreaseTime", lastTime);
   }
   else
   {
     Debug.Log("Last time is " + lastTime);
   }

For working with time, see this: http://stackoverflow.com/questions/2883576/how-do-you-convert-epoch-time-in-c

For the Player Prefs, see this: http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html

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

21 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

Related Questions

Increase energy bar by collecting item, partially working 0 Answers

This not reading folders in the Windows Store 0 Answers

System.IO in javascript 1 Answer

How to Implement System.StringBuilder into JavaScript? 2 Answers

Energy or lives regen by time 1 Answer


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