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 Rumisoft · Jan 11, 2015 at 06:06 PM · save datalife

Life System need help please!!!

alt text

Hello I have big trouble making an lives system . I need a script that give me 5 lives and when i lose a life start recharging 10 minutes even if quit the game.I`ve got only a script with 50% of the code i need and isnt even working. Im stuck on this 3 days please someone help me make this energy system work.

using UnityEngine; using System.Collections; using System;

public class Life : MonoBehaviour {

 public GUIText datee;

 public int life;
 public int lifenumber=5;
 public int timetowait=5;
 public string thesave ="whenlife5back";

 private bool stuffdone;
 private bool stuffdonetwo;


 DateTime currentDate;
 DateTime whenlostLife;
 DateTime whenlife5back;



 void Start()
 {
     stuffdonetwo = true;
     life = PlayerPrefs.GetInt ("life");


     long temp = Convert.ToInt64(PlayerPrefs.GetString(thesave));
     

     whenlife5back = DateTime.FromBinary(temp);
     datee.text=("whenlife5back: " + whenlife5back);


     //Use the Subtract method and store the result as a timespan variable
     //TimeSpan difference = currentDate.Subtract(whenlife5back);
     //print("Difference: " + difference.Minutes+","+difference.Seconds);


 }
 void Update (){

         
     if (System.DateTime.Now > whenlife5back) {
         Debug.Log ("true");
         stuffdone=false;
         stuffdonetwo=true;
     }




             Debug.Log (whenlife5back);
             if (life < lifenumber && !stuffdone) {
                     Debug.Log ("changedinupdate");
                     whenlostLife = System.DateTime.Now;
                     whenlife5back = whenlostLife.AddMinutes (05*lifenumber);

                     Debug.Log (whenlife5back);
                     stuffdonetwo = false;
                     stuffdone = true;


             }
             if (life == lifenumber)
                     stuffdone = false;
                 


             if (System.DateTime.Now > whenlife5back && life < lifenumber&&!stuffdone) {
                     EnergySystem.lifes++;
                     life++;
                     stuffdone = true;
                 

     
                     }

}

 void OnApplicationQuit()
 {
     //Savee the current system time as a string in the player prefs class
     PlayerPrefs.SetString(thesave, whenlife5back.ToBinary().ToString());
     PlayerPrefs.SetInt ("life", life);
     print("Saving this date to prefs: " + whenlife5back);
 }
 

}

untitled-1.png (6.9 kB)
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 Priyanka-Rajwanshi · Apr 01, 2020 at 10:51 AM

@Rumisoft You can use the following line of code: For understanding the code, check out http://codesaying.com/life-counter-in-unity/

using System; using System.Collections; using UnityEngine;

public class LifeCounter : MonoBehaviour {

 int _lives;

 DateTime  timeOfPause;

 int maxLives = 5;

 public double timerForLife;

 float lifeReplenishTime = 600f; //10 minutes = 600 seconds

 public int lives{
     set {
         _lives = value; 
         PlayerPrefs.SetInt("Lives", _lives);
     }
     get {
         return _lives;
     }
 }


 void Awake () {
   
     if(!PlayerPrefs.HasKey("Lives")){
         PlayerPrefs.SetString("LifeUpdateTime", DateTime.Now.ToString());
     }

     lives = PlayerPrefs.GetInt("Lives", maxLives);

     //update life counter only if lives are less than maxLives
     if (lives < maxLives)
     {
         float timerToAdd = (float)(System.DateTime.Now - Convert.ToDateTime(PlayerPrefs.GetString("LifeUpdateTime"))).TotalSeconds;
         UpdateLives(timerToAdd);
     }
   
 }
 

 private void Update()
 {
     if (lives < maxLives)
     {
         timerForLife += Time.deltaTime;
         if (timerForLife > lifeReplenishTime)
         {
             UpdateLives(timerForLife);
         }
     }

 }

 void UpdateLives(double timerToAdd ){
     if (lives < maxLives)
     {
         int livesToAdd = Mathf.FloorToInt((float)timerToAdd / lifeReplenishTime);
         timerForLife = (float)timerToAdd % lifeReplenishTime;
         lives += livesToAdd;
         if (lives > maxLives)
         {
             lives = maxLives;
             timerForLife = 0;
         }
         PlayerPrefs.SetString("LifeUpdateTime", DateTime.Now.AddSeconds(-timerForLife).ToString());
     }else{
         PlayerPrefs.SetString("LifeUpdateTime", DateTime.Now.ToString());

     }
 }

 void OnApplicationPause(bool isPause)
 {
     if (isPause)
     {
         timeOfPause = System.DateTime.Now;
     }
     else
     {
         if(timeOfPause == default(DateTime)){
             timeOfPause = System.DateTime.Now;
         }
         float timerToAdd = (float)(System.DateTime.Now - timeOfPause).TotalSeconds;
         timerForLife += timerToAdd;
         UpdateLives(timerForLife);

     }
 }

}

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

I can't save because of the this keyword 0 Answers

How to increase the life of the player? 2 Answers

Lives aren't subtracting, going straight to game over. 2 Answers

internal persistentDataPath with WRITE_EXTERNAL_STORAGE permission 4 Answers

Reload a level while keeping in mind some var 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