I don’t know what’s going on. It was not working. then I got it to work. and now it’s not working again. I believe it’s the PlayerPrefs. even converting does not work.
Here is my code. Like I said it was working then not then working again now it’s broken again.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
using System;
using UnityEngine.UI;
public class MainMenu : MonoBehaviour
{
[SerializeField] private TMP_Text highScoreText;
[SerializeField] private TMP_Text lifeText;
[SerializeField] private int maxLife;
[SerializeField] private int lifeRechargeDuration;
[SerializeField] private AndroidNotificationHandler androidNotificationHandler;
[SerializeField] private IOSNotificationHandler iosNotificationHandler;
[SerializeField] private Button playButton;
private int life;
private const string LifeKey = "Life";
private const string LifeReadyKey = "LifeReady";
public void Play()
{
if(life < 1){return;}
// decrease one life because we are playing
life --;
PlayerPrefs.SetInt(LifeKey, life);
if(life == 0)
{
DateTime lifeReadyFromNow = DateTime.Now.AddMinutes(lifeRechargeDuration);
PlayerPrefs.SetString(LifeReadyKey, lifeReadyFromNow.ToString());
//This will only do this for android
#if UNITY_ANDROID
//The Notification is beeing sent
androidNotificationHandler.SchedulNotification(lifeReadyFromNow);
#elif UNITY_IOS
iosNotificationHandler.SchedulNotification(lifeRechargeDuration);
#endif
}
SceneManager.LoadScene(1);
}
//private void Start()
//{
//OnApplicationFocus(true);
//}
//This function is for if the app is minumized and or maximized.
//We want to keep time even if the app in minumized and the maxamized
private void Start()
{
//if (!hasFocus) {return;}
//If the application is minimized the stop the invoke so it does not stop time,
//And we only have one Invoke to stop. otherwise we could specify like CancelInvoke("#$6");
//CancelInvoke();
int highScore = PlayerPrefs.GetInt(ScoreSystem.HightScoreKey,0);
highScoreText.text = $"Your High Score is: {highScore}".ToString();
life = PlayerPrefs.GetInt(LifeKey, maxLife);
//see if life is out then set the durration for a new life.
if(life == 0)
{
string lifeReadyString = PlayerPrefs.GetString(LifeReadyKey, string.Empty);
Debug.Log($"{lifeReadyString}".ToString());
// see if the string is really empty
if(lifeReadyString == string.Empty){
lifeReadyString = "LifeReady"; //<--- Manually did this
//return;
} // this should not happen, just a check.
//Take a string and changing it to an int for a time.
DateTime lifeReadyTime = DateTime.Parse(lifeReadyString); //<-- Errors here now after manualy making the lifeReadyString toa string value
//This if only works when you start the game and do not close it
if(DateTime.Now > lifeReadyTime)
{
life = maxLife;
PlayerPrefs.SetInt(LifeReadyKey, life);
}
//This else will allow us to check even if the app is not closed.
//else
//{
//playButton.interactable = false;
//Invoke(nameof(LifeRecharged),(lifeReadyTime - DateTime.Now).Seconds);
//}
}
lifeText.text = $"Play ({life})";
}
//private void LifeRecharged()
//{
//playButton.interactable = true;
//life = maxLife;
//PlayerPrefs.SetInt(LifeReadyKey,life);
//lifeText.text = $"Play ({life})";
//}
}
This will fail if PlayerPrefs
have never been set. If the lifeReadyString
is empty you are setting it to ‘LifeReady’ which is not a valid date and will break when you try to parse it into DateTime
.
Appart from that, I’m not sure what is not working
So basically you are saying just set it? I will look in the tutorials code to see if this is being set.
Thank you.
So what I found out is that the string was empty. so what I did was added one life before it checked to see if the string was empty. that worked. i also now know that the Start method is called 2 times. as shown here.
so I think maybe the game did not retain my lifeReadyString value until i got the game going at least one time?
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.