No Life when starting game

Hello,
This was working like a week ago. Now I get no lives when I play my game.

Here is my code from the main menu

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.SceneManagement;

using TMPro;

using System;

public class MainMenu : MonoBehaviour

{

[SerializeField] private TMP_Text highScoreText;

[SerializeField] private TMP_Text lifeText;

[SerializeField] private int maxLife;

[SerializeField] private int lifeRechargeDuration;

#if UNITY_ANDROID

[SerializeField] private AndroidNotificationHandler androidNotificationHandler;

#endif

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);

    //#endif

}

    SceneManager.LoadScene(1);

   

}

private void Start()

{

   int highScore = PlayerPrefs.GetInt(ScoreSystem.HightScoreKey,0);

   highScoreText.text = $"Your High Score is: {highScore}".ToString();

   life = PlayerPrefs.GetInt(LifeKey,maxLife);

   Debug.Log($"{life} TEST");

   //see if life is out then set the durration for a new life.

   if(life == 0)

   {

     string lifeReadyString = PlayerPrefs.GetString(LifeReadyKey,string.Empty);

       

     // see if the string is really empty

     if(lifeReadyString == string.Empty){

        Debug.Log($"you have {life} many lives");

        return;

        } // this should not happen, just a check.

        //Take a string and changing it to an int fro a time.

        DateTime lifeReadyTime =  DateTime.Parse(lifeReadyString);

        if(DateTime.Now > lifeReadyTime)

        {

            life = maxLife;

            PlayerPrefs.SetInt(LifeReadyKey,life);

        }

   }

   lifeText.text = $"Play ({life})";

}

}

Disregard I found the solution
in my code Here:
string lifeReadyString = PlayerPrefs.GetString(LifeReadyKey,string.Empty);

there was no space between the “,” and string.Empty.
it should be
string lifeReadyString = PlayerPrefs.GetString(LifeReadyKey, string.Empty);

such a small thing threw everything off.

Thank you.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms