Search Unity

Lives system like in Candy Crush Saga PLEASE HELP!!!!

Discussion in 'Scripting' started by pokobros, Apr 22, 2014.

  1. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    I am trying to implement a lives system in Candy Crush Saga where if you run out of lives then in 30 minutes they will regenerate. Here is the code I have so far. So, what I am doing is taking the current time of when the user runs out of lives. Then the next time they open the app I see what the time is and check to see if 30 minutes have elapsed and then refill the lives.
    This is how I get the time of when the user runs out of lives.
    Code (csharp):
    1. int day = DateTime.Now.Day;
    2.                     PlayerPrefs.SetInt("Day", day);
    3.                     int hours = DateTime.Now.Hour;
    4.                     PlayerPrefs.SetInt("Hour", hours);
    5.                     int mins = DateTime.Now.Minute;
    6.                     PlayerPrefs.SetInt("Minute", mins);
    7.                    
    8.                     PlayerPrefs.SetString("CheckForNewLives", "yes");
    9.                    
    10.                     PlayerPrefs.Save();
    This is how I check to see if 30 minutes have elapsed.
    Code (csharp):
    1. string checkForNewLives = PlayerPrefs.GetString("CheckForNewLives");
    2.         if (checkForNewLives == "yes") {
    3.             int day = DateTime.Now.Day;
    4.             int hours = DateTime.Now.Hour;
    5.             int mins = DateTime.Now.Minute;
    6.            
    7.             int beforeDay = PlayerPrefs.GetInt("Day");
    8.             int beforeHour = PlayerPrefs.GetInt("Hour");
    9.             int beforeMin = PlayerPrefs.GetInt("Minute");
    10.            
    11.             if (day != beforeDay) {
    12.                 PlayerPrefs.SetString("Num of Lives", "5");
    13.             }
    14.             if (day == beforeDay  hours > beforeHour) {
    15.                 PlayerPrefs.SetString("Num of Lives", "5");
    16.             }
    17.             if (hours != beforeHour) {
    18.                 PlayerPrefs.SetString("Num of Lives", "5");
    19.             }
    20.             if (beforeMin <= 30) {
    21.                 if (hours == beforeHour  mins == beforeMin + 30) {
    22.                     PlayerPrefs.SetString("Num of Lives", "5");
    23.                 }
    24.             }else if (beforeMin >= 31  beforeMin <= 40) {
    25.                 if (hours != beforeHour  mins >= 0 || mins <= 10) {
    26.                     PlayerPrefs.SetString("Num of Lives", "5");
    27.                 }
    28.             }else if (beforeMin >= 41  beforeMin <= 50) {
    29.                 if (hours != beforeHour  mins >= 11 || mins <= 20) {
    30.                     PlayerPrefs.SetString("Num of Lives", "5");
    31.                 }
    32.             }else if (beforeMin >= 51  beforeMin <= 59) {
    33.                 if (hours != beforeHour  mins >= 21 || mins <= 29) {
    34.                     PlayerPrefs.SetString("Num of Lives", "5");
    35.                 }
    36.             }
    37.            
    38.         }
    I want to change it to be more like Candy Crush where there is a running timer and once that timer runs out then a push notification is sent saying that the lives have been regenerated. How would I change my code to do that? Right now I do not even think my code does what it is supposed to do :cry: PLEASE HELP!! Thank you very much in advance for all of the help!
     
  2. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    You don't really need to store any of those PlayerPrefs for the date and time stuff. Just store a local variable of a DateTime object. Use DateTime.Now and add 30 minutes to it and store that in another variable when the player runs out of energy. Then in Application_Pause (runs when you lose focus and gain focus on the app) you set the LocalNotification to pop up at the appropriate time, when it loses focus. When it gains focus, you can check if DateTime.Now is more than the variable you stored before.

    Then you can use an NGUI Label or whatever you use for UI. Display time remaining, by setting its text by getting a TImeSpan object by subtracting DateTime.Now from the "energy rebuilt" time and doing .ToString("mm:ss"), and keep doing that every second to update the timer until time runs out. If time ran out that way, or when you first gain focus, give the player energy.

    That's how I do it.
     
    ashfakmeethal and JnPri like this.
  3. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    Hello jerotas I have been looking all over for a good tutorial on how to implement LocalNotifications and Application_Pause into my game. There is none. Can you give me some code examples for me to work with. I am 13 years old and I need some guidance with this.
     
  4. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    I'll send you my code when I get home tonight :)
     
  5. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    Thank you so much! I also had a question on Hey Zap that you recommended to me in another forum post. If you could answer that also it would be greatly appreciated! Thank you again!!
     
  6. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    I probably didn't subscribe to that other thread. Where is it?
     
  7. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
  8. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Here's the code for ApplicationPause and Notifications.

    Code (csharp):
    1.    
    2.     private static DateTime? energyRefillTime = null;
    3.  
    4.     private static void SetEnergyRefillTimer() {
    5.         energyRefillTime = DateTime.Now.AddHours(1);
    6.         var countdownTimerText = "59:59";
    7.         // display countdownTimerText somewhere!
    8.     }
    9.  
    10.     void OnApplicationPause(bool loseFocus) {
    11.         if (loseFocus) {
    12.             SetAllNotifications();
    13.             return;
    14.         }
    15.  
    16.         // gained focus!
    17.         CheckIfEnergyWaitTimeOver();
    18.     }
    19.  
    20.     private void SetAllNotifications() {
    21.         NotificationServices.CancelAllLocalNotifications();
    22.         NotificationServices.ClearLocalNotifications();
    23.  
    24.         if (energyRefillTime.HasValue) {
    25.             var notif = new LocalNotification();
    26.             notif.fireDate = energyRefillTime.Value;
    27.             notif.alertBody = "You have more energy!";
    28.             notif.alertAction = "The wait is over!";
    29.             notif.soundName = LocalNotification.defaultSoundName;
    30.             notif.applicationIconBadgeNumber = 1;
    31.             NotificationServices.ScheduleLocalNotification(notif);
    32.         }
    33.     }
    34.  
    35.     public bool CheckIfEnergyWaitTimeOver() {
    36.         if (!energyRefillTime.HasValue || DateTime.Now < energyRefillTime.Value) {
    37.             if (energyRefillTime.HasValue) {
    38.                 TimeSpan remaining = energyRefillTime.Value - DateTime.Now;
    39.                 var timerCountdownText = string.Format("{0:D2}:{1:D2}", remaining.Minutes, remaining.Seconds);
    40.                 // use timerCountdownText and display it somewhere!
    41.  
    42.                 return false;
    43.             } else {
    44.                 // no refill time set!
    45.             }
    46.  
    47.             return true;
    48.         }
    49.        
    50.         RemoveNotification();
    51.         return true;
    52.     }
    53.  
    54.     public static void RemoveNotification() {
    55.         NotificationHelper.ClearAllBadges();
    56.  
    57.         // Add code to give player more energy!
    58.  
    59.         energyRefillTime = null;
    60.     }
    61.  
    Call SetEnergyRefillTimer() when the player needs to start waiting (energy ran out).

    Then make sure to call CheckIfEnergyWaitTimeOver() every 1 second (or more often) to update the countdown timer text. The rest should "just work".
     
    Last edited: Apr 23, 2014
    JRHidalgo likes this.
  9. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    So jerotas I am trying to implement your code into my app. I declare countdownTimerText like this so I could use it to display in my OnGUI function:
    Code (csharp):
    1. private string countdownTimerText;
    Then I copied and pasted your methods into my class like this:
    Code (csharp):
    1. private static void SetEnergyRefillTimer() {
    2.  
    3.             energyRefillTime = DateTime.Now.AddMinutes(30);
    4.  
    5.             countdownTimerText = "30:00";
    6.  
    7.             // display countdownTimerText somewhere!
    8.  
    9.      }
    10.        
    11.         void OnApplicationPause(bool loseFocus) {
    12.  
    13.                 if (loseFocus) {
    14.  
    15.                     SetAllNotifications();
    16.  
    17.                     return;
    18.  
    19.                 }
    20.  
    21.                 // gained focus!
    22.  
    23.                 CheckIfEnergyWaitTimeOver();
    24.  
    25.             }
    26.            
    27.             private void SetAllNotifications() {
    28.  
    29.                     NotificationServices.CancelAllLocalNotifications();
    30.  
    31.                     NotificationServices.ClearLocalNotifications();
    32.  
    33.  
    34.  
    35.                     if (energyRefillTime.HasValue) {
    36.  
    37.                         var notif = new LocalNotification();
    38.  
    39.                         notif.fireDate = energyRefillTime.Value;
    40.  
    41.                         notif.alertBody = "You have more lives!";
    42.  
    43.                         notif.alertAction = "The wait is over!";
    44.  
    45.                         notif.soundName = LocalNotification.defaultSoundName;
    46.  
    47.                         notif.applicationIconBadgeNumber = 1;
    48.  
    49.                         NotificationServices.ScheduleLocalNotification(notif);
    50.  
    51.                     }
    52.  
    53.                 }
    54.  
    55.  
    56.  
    57.                 public bool CheckIfEnergyWaitTimeOver() {
    58.  
    59.                     if (!energyRefillTime.HasValue || DateTime.Now < energyRefillTime.Value) {
    60.  
    61.                         if (energyRefillTime.HasValue) {
    62.  
    63.                             TimeSpan remaining = energyRefillTime.Value - DateTime.Now;
    64.  
    65.                             string timerCountdownText = string.Format("{0:D2}:{1:D2}", remaining.Minutes, remaining.Seconds);
    66.  
    67.                             // use timerCountdownText and display it somewhere!
    68.  
    69.  
    70.  
    71.                             return false;
    72.  
    73.                         } else {
    74.  
    75.                             // no refill time set!
    76.  
    77.                         }
    78.  
    79.  
    80.  
    81.                         return true;
    82.  
    83.                     }
    84.  
    85.        
    86.  
    87.                     RemoveNotification();
    88.  
    89.                     return true;
    90.  
    91.                 }
    92.  
    93.  
    94.  
    95.                 public static void RemoveNotification() {
    96.  
    97.                     NotificationServices.ClearAllBadges();
    98.  
    99.  
    100.  
    101.                     // Add code to give player more energy!
    102.  
    103.  
    104.  
    105.                     energyRefillTime = null;
    106.  
    107.                 }
    I call SetEnergyRefillTimer() like this:
    Code (csharp):
    1. SetEnergyRefillTimer();
    and I am calling CheckIfEnergyWaitTimerIsOver() like this:
    Code (csharp):
    1. InvokeRepeating("CheckIfEnergyWaitTimeOver", 0.0f, 1.0f);
    I am getting error though that says this: 1.)An object reference is required to access non-static member `countdownTimerText' 2.)The variable `timerCountdownText' is assigned but its value is never used 3.)`UnityEngine.NotificationServices' does not contain a definition for `ClearAllBadges'
     
  10. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Remove the word "static" from the declaration of the SetEnergyRefillTimer method to fix #1.
    #2 is a warning, it will go away when you actually use that variable. Won't stop you from compiling.
    #3, no idea, that compiles for me. I don't know why it's saying that. If you re-type the dot after NotificationServices, do you see a similar named method?
     
  11. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    Would this be able to take the place of
    Code (csharp):
    1. NotificationServices.ClearAllBadges();
    -->
    Code (csharp):
    1. NotificationServices.CancelAllLocalNotifications();
    2.                     NotificationServices.ClearLocalNotifications();
     
  12. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Sure, that looks the same. Try that.
     
  13. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    Works well!
     
  14. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Awesome!
     
  15. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    With the notifications do I need to create provisioning profiles that enable push notifications? Do I need to do anything special for push notifications?
     
  16. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    No, because these are Local Notifications, not push notifications. There's a difference. Don't need to enable anything.
     
  17. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    Ok thank you!!
     
  18. magonicolas

    magonicolas

    Joined:
    Dec 4, 2013
    Posts:
    30
    Hi there! So I Copy the Code and call SetEnergyRefillTimer() at start for testing and un update Called CheckIfWaitTimerOver() but is not working, here is my code.

    Code (CSharp):
    1.  
    2. public class Life2Controller : MonoBehaviour
    3. {
    4.     private static DateTime? energyRefillTime = null;
    5.     public string displayTimerCountDown;
    6.  
    7.  
    8.     void Update ()
    9.     {
    10.         CheckIfEnergyWaitTimeOver();
    11.     }
    12.     void Start ()
    13.     {
    14.         SetEnergyRefillTimer();
    15.     }
    16.    
    17.     private static void SetEnergyRefillTimer() {
    18.         energyRefillTime = DateTime.Now.AddMinutes(1);
    19.         string countdownTimerText = "00:59";
    20.         print (countdownTimerText);
    21.         // display countdownTimerText somewhere!
    22.     }
    23.    
    24.     void OnApplicationPause(bool loseFocus) {
    25.         if (loseFocus) {
    26.             SetAllNotifications();
    27.             return;
    28.         }
    29.        
    30.         // gained focus!
    31.         CheckIfEnergyWaitTimeOver();
    32.     }
    33.    
    34.     private void SetAllNotifications() {
    35.         NotificationServices.CancelAllLocalNotifications();
    36.         NotificationServices.ClearLocalNotifications();
    37.        
    38.         if (energyRefillTime.HasValue) {
    39.             var notif = new LocalNotification();
    40.             notif.fireDate = energyRefillTime.Value;
    41.             notif.alertBody = "You have more energy!";
    42.             notif.alertAction = "The wait is over!";
    43.             notif.soundName = LocalNotification.defaultSoundName;
    44.             notif.applicationIconBadgeNumber = 1;
    45.             NotificationServices.ScheduleLocalNotification(notif);
    46.         }
    47.     }
    48.    
    49.     public bool CheckIfEnergyWaitTimeOver() {
    50.  
    51.         if (!energyRefillTime.HasValue || DateTime.Now < energyRefillTime.Value) {
    52.  
    53.             if (energyRefillTime.HasValue) {
    54.                 TimeSpan remaining = energyRefillTime.Value - DateTime.Now;
    55.                 var timerCountdownText = string.Format("{0:D2}:{1:D2}", remaining.Minutes, remaining.Seconds);
    56.                 // use timerCountdownText and display it somewhere!
    57.                 print (timerCountdownText);
    58.                
    59.                 return false;
    60.             } else {
    61.  
    62.             }
    63.            
    64.             return true;
    65.         }
    66.        
    67.         RemoveNotification();
    68.         return true;
    69.     }
    70.    
    71.     public static void RemoveNotification() {
    72.         NotificationServices.ClearLocalNotifications();
    73.        
    74.         energyRefillTime = null;
    75.     }
    76. }

    Any Clues?
     
  19. musheer

    musheer

    Joined:
    Jun 17, 2015
    Posts:
    9
    may you provide me a full script i am new for unity3D thanx in advance.....
     
  20. saddam751

    saddam751

    Joined:
    Nov 6, 2013
    Posts:
    41
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System.Text;
    5. using System;
    6.  
    7.  
    8. public class LifeFunctionality : MonoBehaviour
    9. {
    10.     //For Life Functionality
    11.     private bool isPause = true;
    12.  
    13.     public Text lifeText;
    14.    
    15.     DateTime currentDate;
    16.     DateTime oldDate;
    17.    
    18.  
    19.     private long temp;
    20.  
    21.     private float StartTime;
    22.     public static int LifeCounter;
    23.     private int TimeDifference;
    24.     public int timeToAdd;
    25.     private int dividesTimeToAddAfterResume;
    26.  
    27.     public GameObject pauseButton;
    28.     public GameObject resumeButton;
    29.  
    30.     private float differencePlusRemainingTime;
    31.     private float totalDifferenceInSeconds;
    32.  
    33.    
    34.     void Start()
    35.     {
    36.         PlayerPrefs.DeleteAll();
    37.  
    38.         //GAME STARTED FIRST TIME SET LIFE COUNTER TO 0
    39.         if (!(PlayerPrefs.HasKey ("LifeCounter")))
    40.         {
    41.             LifeCounter=0;
    42.         }
    43.         else if ((PlayerPrefs.HasKey ("LifeCounter")))
    44.         {
    45.             LifeCounter=PlayerPrefs.GetInt("LifeCounter");
    46.         }
    47.  
    48.         //GAME PLAYER FIRST TIME AND THERE IS NO SCENE SWITCHING
    49.         if (!(PlayerPrefs.HasKey ("RunningGameTimeToAdd")))
    50.         {
    51.             timeToAdd = 0;
    52.             StartTime = Time.timeSinceLevelLoad;
    53.         }
    54.  
    55.         //IF SCENE SWITCHING OCCURED , WE ARE STORING THE TIME IN timetoadd INT AND UPDATING LIFE COUNTER
    56.         else if ((PlayerPrefs.HasKey ("RunningGameTimeToAdd")))
    57.         {
    58.             LifeCounter=PlayerPrefs.GetInt("LifeCounter");
    59.  
    60.             StartTime = Time.timeSinceLevelLoad;
    61.             timeToAdd=(PlayerPrefs.GetInt("RunningGameTimeToAdd"));
    62.             Debug.Log("Remaining Time On Scene Switching-Level-1------->"+ timeToAdd);
    63.         }
    64.  
    65.     }
    66.    
    67.    
    68.     void FixedUpdate()
    69.     {
    70.     //    Debug.Log((int)Time.timeSinceLevelLoad);
    71.         TimeDifference = (int)((Time.timeSinceLevelLoad+timeToAdd) - StartTime);
    72.     /*    Debug.Log("totalDifferenceInSeconds---->"+totalDifferenceInSeconds);
    73.         Debug.Log("timeToAdd---->"+timeToAdd);
    74.         Debug.Log("ADD---->"+(totalDifferenceInSeconds +timeToAdd));
    75.     //    Debug.Log("Time.Time---->"+(int)Time.timeSinceLevelLoad);
    76. //        Debug.Log("StartTime---->"+StartTime);
    77.         Debug.Log("Time Difference---->"+TimeDifference);*/
    78.  
    79.  
    80.         if (TimeDifference >= 10)
    81.         {
    82.             timeToAdd = 0;
    83.             StartTime = Time.timeSinceLevelLoad;
    84.            
    85.             if(LifeCounter < 5)
    86.             {
    87.                 LifeCounter += 1;
    88.  
    89. //                Debug.Log("LIFE==="+LifeCounter);
    90.             }
    91.         }
    92.        
    93.         lifeText.text =""+LifeCounter;
    94.  
    95.  
    96.     }
    97.    
    98.     //THIS IS CALLED WHEN GAME IS RESUMED FROM PAUSE
    99.     void CalculateTimeDifference()
    100.     {
    101.         //Store the current time when it starts
    102.         currentDate = System.DateTime.Now;
    103.         Debug.Log("Current Date----->"+currentDate);
    104.        
    105.         //Grab the old time from the player prefs as a long
    106.         long temp = Convert.ToInt64(PlayerPrefs.GetString("TimeSpent_OnApplicationPause"));
    107.        
    108.         //Convert the old time from binary to a DataTime variable
    109.         DateTime oldDate = DateTime.FromBinary(temp);
    110.         print("oldDate: " + oldDate);
    111.        
    112.         //Use the Subtract method and store the result as a timespan variable
    113.         TimeSpan difference = currentDate.Subtract(oldDate);
    114.         print("Difference: " + difference);
    115.        
    116.         string highestPriority = difference.ToString();
    117.         //Debug.Log(highestPriority);
    118.        
    119.         String[] priorityCache= highestPriority.Split(":"[0]);
    120.        
    121.         int getHour;
    122.         getHour=(int.Parse(priorityCache[0]));
    123.         int hoursInSec;
    124.         hoursInSec=getHour*3600;
    125.         Debug.Log("getHour----------> "+getHour);
    126.         Debug.Log("hoursInSec----------> "+hoursInSec);
    127.  
    128.  
    129.        
    130.         int getMin;
    131.         getMin=(int.Parse(priorityCache[1]));
    132.         int minInSec;
    133.         minInSec=getMin*60;
    134.         Debug.Log("getMin----------> "+getMin);
    135.         Debug.Log("minInSec----------> "+getMin);
    136.  
    137.  
    138.         float getSec;
    139.         getSec=((int)float.Parse(priorityCache[2]));
    140.         Debug.Log("getSec---------->"+getSec);
    141.  
    142.         totalDifferenceInSeconds=(int)(hoursInSec + minInSec + getSec);
    143.         Debug.Log("TOTAL DIFFERENCE IN SECONDS---->"+totalDifferenceInSeconds);
    144.  
    145.         differencePlusRemainingTime=(int)(PlayerPrefs.GetInt("RunningGameTimeToAdd") + totalDifferenceInSeconds);
    146.         Debug.Log("TOTAL DIFFERENCE PLUS REMAINING TIME---->"+differencePlusRemainingTime);
    147.  
    148.         timeToAdd = (int)(differencePlusRemainingTime % 10);
    149.         Debug.Log("TIME TO ADD AFTER RESUME---------> "+timeToAdd);
    150.  
    151.  
    152.         if(LifeCounter < 5)
    153.         {
    154.             LifeCounter += (int)(differencePlusRemainingTime / 10);
    155.             Debug.Log("RESUME----> "+LifeCounter);
    156.  
    157.             if(LifeCounter > 5)
    158.             {
    159.                 LifeCounter = 5;
    160.             }
    161.         }
    162.  
    163.     }
    164.  
    165.  
    166.  
    167.  
    168.  
    169. /*    void OnApplicationQuit()
    170.     {
    171.         //Save the current system time as a string in the player prefs class
    172.  
    173.     }*/
    174.     public void loadLevel1()
    175.     {
    176.         PlayerPrefs.SetInt("RunningGameTimeToAdd", TimeDifference);
    177.         Debug.Log("RunningGameTimeToAdd------>"+PlayerPrefs.GetInt("RunningGameTimeToAdd"));
    178.  
    179.         PlayerPrefs.SetInt("LifeCounter",LifeCounter);
    180.        
    181.         Debug.Log("Remaining Time On Scene Switching------->"+ PlayerPrefs.GetInt("RunningGameTimeToAdd"));
    182.         Application.LoadLevel("Scene1");
    183.     }
    184.     public void loadLevel2()
    185.     {
    186.         PlayerPrefs.SetInt("RunningGameTimeToAdd", TimeDifference);
    187.         PlayerPrefs.SetInt("LifeCounter",LifeCounter);
    188.  
    189.         Debug.Log("Remaining Time On Scene Switching------->"+ PlayerPrefs.GetInt("RunningGameTimeToAdd"));
    190.         Application.LoadLevel("Scene2");
    191.     }
    192.     public void quitPressed()
    193.     {
    194.         PlayerPrefs.SetInt("RunningGameTimeToAdd", TimeDifference);
    195.         PlayerPrefs.SetInt("LifeCounter",LifeCounter);
    196.  
    197.         Debug.Log("Remaining Time On Scene Switching------->"+ PlayerPrefs.GetInt("RunningGameTimeToAdd"));
    198.         Application.LoadLevel("Home");
    199.     }
    200.  
    201.     public void level1()
    202.     {
    203.         PlayerPrefs.SetInt("RunningGameTimeToAdd", TimeDifference);
    204.         PlayerPrefs.SetInt("LifeCounter",LifeCounter);
    205.        
    206.         Application.LoadLevel("Scene1");
    207.     }
    208.     public void level2()
    209.     {
    210.         PlayerPrefs.SetInt("RunningGameTimeToAdd", TimeDifference);
    211.         PlayerPrefs.SetInt("LifeCounter",LifeCounter);
    212.        
    213.         Debug.Log("Remaining Time On Scene Switching------->"+ PlayerPrefs.GetInt("RunningGameTimeToAdd"));
    214.         Application.LoadLevel("Scene2");
    215.     }
    216.  
    217.     public void pauseGame()
    218.     {
    219.         PlayerPrefs.SetString("TimeSpent_OnApplicationPause", System.DateTime.Now.ToBinary().ToString());
    220.         PlayerPrefs.SetInt("RunningGameTimeToAdd", TimeDifference);
    221.  
    222.         Debug.Log("Remaining Time On Scene Switching------->"+ PlayerPrefs.GetInt("RunningGameTimeToAdd"));
    223.  
    224.         PlayerPrefs.SetInt("LifeCounter",LifeCounter);
    225.  
    226.         resumeButton.SetActive(true);
    227.         pauseButton.SetActive(false);
    228.  
    229.         Time.timeScale=0;
    230.     }
    231.  
    232.     public void ResumeGame()
    233.     {
    234.         LifeCounter=PlayerPrefs.GetInt("LifeCounter");
    235.  
    236.         if(PlayerPrefs.HasKey("TimeSpent_OnApplicationPause"))
    237.             CalculateTimeDifference();
    238.  
    239.         pauseButton.SetActive(true);
    240.         resumeButton.SetActive(false);
    241.         Time.timeScale=1;
    242.     }
    243. }
     
  21. saddam751

    saddam751

    Joined:
    Nov 6, 2013
    Posts:
    41
    This script works fine, only problem is when user pause and resume game randomly, life counter increases before given time i.e. 10 sec in my case
     
  22. terrelldot

    terrelldot

    Joined:
    Jul 27, 2015
    Posts:
    1
    How did you make your life system
     
  23. Humanity

    Humanity

    Joined:
    Mar 6, 2015
    Posts:
    1
    Any code above is not a perfect one.
     
  24. chrisMary

    chrisMary

    Joined:
    Nov 27, 2013
    Posts:
    16
    Hi,
    I'm starting mine right now (that's how I ended up here ^^).

    I plan on creating a list of 5 DateTimes, and when the player dies, find the first one in the list inferior to Now, and set it to Now + X minuts.

    Then, to get the number of lives, a simple function that returns the number of datetimes inferior to Now.

    And store that list :)
     
  25. zero_null

    zero_null

    Joined:
    Mar 11, 2014
    Posts:
    159
    hey guys !
    any help?
    I am in the same situation !
     
  26. uzairamirs

    uzairamirs

    Joined:
    Jan 7, 2015
    Posts:
    8
    Hi everyone,
    The code Mr. @jerotas wrote here, is this only for Apple devices? I want to do the same for Android devices and want to send a notification when the energy bar is filled. How'd I do that? I can not see anything like RemoveNotification or something else here in Visual Studio 2015....
    Please help.
     
  27. neciogames

    neciogames

    Joined:
    Jan 3, 2013
    Posts:
    7
    uzairamirs likes this.
  28. Tarmack

    Tarmack

    Joined:
    Dec 16, 2016
    Posts:
    12
    This code is not good at all, because the timers are not correct, if you have a game with lifes and max lifes let's take for example 3 as max life.
    if you are starting Regen for the 1st life at 00h00 and your regen timer is 1min, so logically your life bar should be full at 00H03 but in this case, if the user quit the game and come back at like 00H06 if he leaved at 00H00:30 sec
    he'll won one life instead of 3
     
  29. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Yeah you may need to revise for cases like that.