- Home /
Life System need help please!!!
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
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);
}
}
}