I am working in a new game that is almost finished, but i cant get my script to work correctly to give me a bestTime rather than the currentTime I am saving result in PlayerPrefs and i have various if()s to verify all cases…
for example:
float _bestTime_F starts with no value, meaning 0 at the first run of the game.
I have a float _currentTime_F that is constantly updating every game loop.
- I need that if my _currentTime_F is > than my _bestTime_F,
meaning your best time still same because you get a higher time than you Best. - Also verify if my _currentTime_F is < than my _besTime_F,
meaning that you have made a new Best Score… - And if my _bestTime_F is <= _currentTime_F…
player Best Time maintain the _bestTime_F value…
meaning player have finished the level and no new Best Time has been made.
I will appreciate any advice. ::JCD
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class _Timer : MonoBehaviour
{
// VARIABLES____________________...
public Text timer_TXT;
private string _currentTime_S;
private float _currentTime_F;
public Text bestTime_TXT;
public string bestTime_S;
private float _bestTime_F;
// FUNCTIONS_____________________...
void Start()
{
//_bestTime_F = PlayerPrefs.GetFloat("BestTime", Mathf.Infinity);
_bestTime_F = PlayerPrefs.GetFloat("BestTime");
Debug.Log(_bestTime_F);
} //start.
private void Update()
{
Timer();
} //update.
private void Timer()
{
timer_TXT.text = System.TimeSpan.
FromSeconds((int)Time.timeSinceLevelLoad).
ToString();
_currentTime_S = timer_TXT.text;
_currentTime_F += Time.deltaTime;
//Debug.Log(_currentTime_F);
if (_bestTime_F < _currentTime_F)
{
_bestTime_F = _currentTime_F;
}
if (_Spikes.s_destroyed_B) /* testing when player dies but it will be called when level clear */
{
if (_bestTime_F < _currentTime_F)
{
_bestTime_F = _currentTime_F;
PlayerPrefs.SetFloat("BestTime", _bestTime_F);
//PlayerPrefs.Save();
Debug.Log("Your BestTime starts being less than your CurrentTime...");
}
if (_currentTime_F >= _bestTime_F)
{
//_bestTime_F = _currentTime_F;
//_bestTime_F = PlayerPrefs.GetFloat("BestTime");
PlayerPrefs.SetFloat("BestTime", _bestTime_F);
//PlayerPrefs.Save();
Debug.Log("Your BestTime is greater or equal than your CurrentTime...");
}
if (_currentTime_F < _bestTime_F)
{
//_bestTime_F = _currentTime_F;
PlayerPrefs.SetFloat("BestTime", _bestTime_F);
Debug.Log("Congratulations, you set a new BestTime!");
}
}
} //timer.
} // END TIMER.
5497273–563653–_Timer.cs (2.24 KB)