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 falcko · Oct 25, 2015 at 04:54 PM · playerprefsenergy

Energy buildup system

Hi!

I'm creating a new 2d game and every time a player starts/reloads a level the game takes down 1 energy point for the collection a energy points (max. is 20 points). Every 10 minutes the player should be getting an extra energy point until the maximum of 20 is reached.

I have this sort of working with the use of playerPrefs but I'm not sure this is the right way of doing it.

How would you guys get energy in the game?

Cheers!

Here is my code so far:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System;
 
 public class EnergyManager : MonoBehaviour {
 
     public Text energyLevelText;
 
     // Use this for initialization
     void Start () {
 
         //check if prefs are set
         if (!PlayerPrefs.HasKey ("maxEnergy")) {
             PlayerPrefs.SetInt("maxEnergy",20);
         }
 
         if (!PlayerPrefs.HasKey ("lastTimeUsed")) {
             PlayerPrefs.SetString("lastTimeUsed", System.DateTime.Now.ToBinary().ToString());
         }
 
         if (!PlayerPrefs.HasKey ("energyLevel")) {
             PlayerPrefs.SetInt("energyLevel", 20);
         }
 
         InvokeRepeating ("RecalculateEnergy", 0.0F, 10.0F);
 
     
     }
 
     public void RecalculateEnergy() {
         //Store the current time when it starts
         DateTime currentDate = System.DateTime.Now;
         //Grab the old time from the player prefs as a long
         long temp = Convert.ToInt64(PlayerPrefs.GetString("lastTimeUsed"));
         //Convert the old time from binary to a DataTime variable
         DateTime oldDate = DateTime.FromBinary(temp);
         //Use the Subtract method and store the result as a timespan variable
         TimeSpan difference = currentDate.Subtract(oldDate);
         Debug.Log ("Difference " + difference);
 
         Debug.Log ( (difference.TotalMinutes / 10) - (difference.TotalMinutes % 10));
 
         int energyToAdd = (int)(difference.TotalMinutes / 10) - (int)(difference.TotalMinutes % 10);
 
         //Mathf.Round(
 
         if (energyToAdd > 0) {
             AddEnergy(1);
         }
 
         energyLevelText.text = GetEnergy ().ToString ();
 
     }
 
 
 
     public int GetEnergy() {
         //RecalculateEnergy ();
         return PlayerPrefs.GetInt ("energyLevel");
     }
 
 
 
     public void UseEnergy() {
 
 
         int energyLevel = GetEnergy ();
 
         if (energyLevel > 0) {
 
             //Store the current time when it starts
             DateTime currentDate = System.DateTime.Now;
             
             //Grab the old time from the player prefs as a long
             long temp = Convert.ToInt64(PlayerPrefs.GetString("lastTimeUsed"));
             
             //Convert the old time from binary to a DataTime variable
             DateTime oldDate = DateTime.FromBinary(temp);
             Debug.Log("oldDate: " + oldDate);
             
             //Use the Subtract method and store the result as a timespan variable
             TimeSpan difference = currentDate.Subtract(oldDate);
             Debug.Log("Difference: " + difference.TotalMinutes);
 
             if (difference.Minutes>=10) {
                 PlayerPrefs.SetString("lastTimeUsed", System.DateTime.Now.ToBinary().ToString());
             }
 
             energyLevel--;
             PlayerPrefs.SetInt("energyLevel", energyLevel);
 
             energyLevelText.text = GetEnergy ().ToString ();
         }
 
         Debug.Log ("Energy used : energylevel " + energyLevel + " last time : " + PlayerPrefs.GetString("lastTimeUsed"));
         RecalculateEnergy ();
 
     }
 
     public void AddEnergy(int energyToAdd) {
         int newEnergy = GetEnergy() + energyToAdd;
         if (newEnergy > PlayerPrefs.GetInt("maxEnergy")) {
             newEnergy = PlayerPrefs.GetInt("maxEnergy");
 
         }
         
         PlayerPrefs.SetInt("energyLevel", newEnergy);
         PlayerPrefs.SetString("lastTimeUsed", System.DateTime.Now.ToBinary().ToString());
         RecalculateEnergy ();
     }
     
 
 
 
 }
 
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 falcko · Nov 17, 2015 at 03:02 PM

Nobody has an idea?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

how do i store highscore locally C# (SIMPLE) 3 Answers

Saving item count with Playerprefs 1 Answer

How do I get a serialized field saved into a PlayerPref? 0 Answers

SetActive dosen't work 0 Answers

Mute Toggle Not Staying On 2 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