Question by
Januar · Mar 13, 2021 at 04:37 PM ·
playerprefssavetimer
PlayerPref not save the fastest time
I am making a simple memory game where the fastest time is saved and appears in the main menu. But for some reason time is not saved. Bigger float is saved but smaller float not.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Timer : MonoBehaviour
{
public Text timerText;
public Text newRecord;
public bool playing;
public int numberOfBoxes;
public GameObject setupCanvas;
public float timer;
void Update()
{
if (playing)
{
timer += Time.deltaTime;
int minutes = Mathf.FloorToInt(timer / 60f);
int seconds = Mathf.FloorToInt(timer % 60f);
int milliseconds = Mathf.FloorToInt((timer * 100f) % 100f);
timerText.text = minutes.ToString("00") + ":" + seconds.ToString("00") + ":" + milliseconds.ToString("00");
}
if (GameObject.FindWithTag("Box") == null)
{
playing = false;
TimeStopped();
}
}
void TimeStopped()
{
if (numberOfBoxes == 10)
{
if (PlayerPrefs.GetFloat("BestTimeFloat10") >= timer)
{
PlayerPrefs.SetFloat("BestTimeFloat10", timer);
PlayerPrefs.SetString("BestTimeString10", timerText.text.ToString());
newRecord.text = "NEW RECORD!";
}
else
{
newRecord.text = "";
}
}
setupCanvas.SetActive(true);
}
public void GoMainMenu()
{
SceneManager.LoadScene("Menu");
}
public void ResetGame()
{
Scene scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name);
}
}
Comment