Hi there,
I am creating my first game and want to add a timer to it and it worked but when i now try to save the Time via PlayerPrefs it dosnt work. This is the first time I’am using PlayerPrefs so I really need help to understand it.
The code for the Timer:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SafeBestTime : MonoBehaviour
{
public Text timer;
public Text finishedTime;
public float timeStart;
public bool IsNotPaused = true;
public bool GameIsCom;
void Start()
{
PlayerPrefs.SetFloat("TheBestTime", timeStart);
}
void Update()
{
if (Input.GetKey(KeyCode.Q))
{
ResetBestTime();
}
if (IsNotPaused)
{
timeStart += Time.deltaTime;
timer.text = timeStart.ToString("F2");
}
if (GameIsCom)
{
SafeTime();
}
}
void SafeTime()
{
if (timeStart > PlayerPrefs.GetFloat("TheBestTime", timeStart))
{
PlayerPrefs.SetFloat("TheBestTime", timeStart);
Debug.Log(PlayerPrefs.GetFloat("TheBestTime", timeStart));
PlayerPrefs.Save();
finishedTime.text = PlayerPrefs.GetFloat("TheBestTime", timeStart).ToString();
}
}
void ResetBestTime()
{
PlayerPrefs.DeleteKey("TheBestTime");
}
}