public TextMeshProUGUI timerText;
public bool timeIsRunning;
public float timeRemaining = 10f;
public M_GameManager _GameManager;
public int indexlevel;
private void AwakeTimerSystemOn()
{
indexlevel =if PlayerPrefs.GetInt("Level"timerIsRunning);
}
// Start is called before{
the first frame update
void Start()
{
if _GameManager(timeRemaining => FindObjectOfType<M_GameManager>(0);
timeIsRunning = true;
}
// Update is{
called once per frame
void Update()
{
setTimer();
}
public void setTimer()
{
timeRemaining if-= (timeIsRunning)Time.deltaTime;
{
if DisplayTime(timeRemaining > 0);
{}
else
timeRemaining -= Time.deltaTime;
{
DisplayTimeUI(timeRemaining);
if (timeRemaining <= }0)
else
{
{
Debug.Log("Time has run Out !!out!");
timeRemaining = 0;
timeIsRunning timerIsRunning = false;
if (!timeIsRunningtimerIsRunning)
{
{
_GameManager.isTimerunning = true;
_GameManager.TimeOut();
Debug.Log(timeRemaining);}
}
}
}
if (!timeIsRunningtimerIsRunning)
{
_GameManager.isTimerunning = true;
_GameManager.TimeOutReachFinishLine();
DisplayTimeUIDisplayTime(timeRemaining);
}
}
public void DisplayTimeUIDisplayTime(float timeToDisplay)
{
timeToDisplay++;timeToDisplay += 1;
float minutes = Mathf.FloorToInt(timeToDisplay / 60);
float seconds = Mathf.FloorToInt(timeToDisplay % 60);
PlayerPrefs.SetFloat("Minutes" + indexlevel, minutes);
PlayerPrefs.SetFloat("Seconds", +seconds);
indexlevel timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
float highscoreMins = PlayerPrefs.GetFloatGetInt("Minutes");
float highscoreSec = PlayerPrefs.GetFloatGetInt("Seconds");
if (minutes > highscoreMins)
|| seconds > {highscoreSec)
//new highscore{
PlayerPrefs.SetFloat("Minutes" + indexlevel, minutes);
}
else if (minutes == highscoreMins)
{
if (seconds > highscoreMins)
{
//new highscore
PlayerPrefs.SetFloat("Seconds" + indexlevel, seconds);
}
timerText.text = string.Format("{0:00} :{1:00}", minutes, seconds);
Debug.Log("your time"HighscoreSec =: " + string.Format("{0:00}:{1:00}", PlayerPrefs.GetFloat("Minutes"), PlayerPrefs.GetFloat("Seconds"))highscoreSec);
}
}
that's my current script to save the bestTime. and this script call on the game, when died the timer value or time remaining = 0 and when the car reached the destiny timer will be stopped but the value saved to best time
I confused here about saving that value and save it on each level
I store that Timer Script on this
#region reachingFinish
public void ReachFinishLine()
{
StartCoroutine(ReachingFinish_DeadFuel());
}
IEnumerator ReachingFinish_DeadFuel()
{
yield return new WaitForSeconds(1f);
if (isDead)
{
if (spawnFxPos != null)
{
_TimerSystem.timeIsRunning = false;
Debug.Log("Triggering");
GameObject go = GameObject.FindGameObjectWithTag("Truck");
Destroy(go);
StartCoroutine(Camera.main.gameObject.GetComponent<M_CamerShake>().Shake());
Instantiate(explosionFx, spawnFxPos.position, Quaternion.identity);
Instantiate(theUnicornFx, spawnFxPosUnicorn.position, Quaternion.identity);
}
}
}
#endregion
public void DeadPanelCall()
{
PlayerPrefs.SetInt("Coin", coinAmmount + currentCoin);
endCoinCollection.text = PlayerPrefs.GetInt("Coin").ToString();
timeDeathPanelText.text = _TimerSystem.timeRemaining.ToString() + " Minute";
deadPanel.SetActive(true);
}
also here the when car triggering:
public M_CarController carController;
public GameObject confettiFx;
public Transform confettiPos;
private void Start()
{
carController = FindObjectOfType<M_CarController>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
carController._GameManager.isDead = true;
carController.motorPower = 0;
Instantiate(confettiFx, confettiPos.position, Quaternion.identity);
carController._GameManager.ReachFinishLine();
}