Skip to main content
added 4 characters in body
Source Link
 public void TimerSystemOn()
{
    if (timerIsRunning)
    {
        if (timeRemaining > 0)
        {
            timeRemaining -= Time.deltaTime;
            DisplayTime(timeRemaining);
        }
        else
        {
            if (timeRemaining <= 0)
            {
                Debug.Log("Time has run out!");
                timeRemaining = 0;
                timerIsRunning = false;

                if (!timerIsRunning)
                {
                    TimeOut();
                }
            }
        }
    }

    if (!timerIsRunning)
    {
        ReachFinishLine();
        DisplayTime(timeRemaining);

    }
}

void DisplayTime(float timeToDisplay)
{
    timeToDisplay += 1;

    float minutes = Mathf.FloorToInt(timeToDisplay / 60);
    float seconds = Mathf.FloorToInt(timeToDisplay % 60);

    PlayerPrefs.SetFloat("Minutes", minutes);
    PlayerPrefs.SetFloat("Seconds", seconds);
    timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);

    float highscoreMins = PlayerPrefs.GetIntGetFloat("Minutes");
    float highscoreSec = PlayerPrefs.GetIntGetFloat("Seconds");

    if (minutes > highscoreMins || seconds > highscoreSec)
    {
        PlayerPrefs.SetFloat("Minutes", minutes);
        PlayerPrefs.SetFloat("Seconds", seconds);
    }
    Debug.Log("HighscoreSec : " + highscoreSec);
}
 public void TimerSystemOn()
{
    if (timerIsRunning)
    {
        if (timeRemaining > 0)
        {
            timeRemaining -= Time.deltaTime;
            DisplayTime(timeRemaining);
        }
        else
        {
            if (timeRemaining <= 0)
            {
                Debug.Log("Time has run out!");
                timeRemaining = 0;
                timerIsRunning = false;

                if (!timerIsRunning)
                {
                    TimeOut();
                }
            }
        }
    }

    if (!timerIsRunning)
    {
        ReachFinishLine();
        DisplayTime(timeRemaining);

    }
}

void DisplayTime(float timeToDisplay)
{
    timeToDisplay += 1;

    float minutes = Mathf.FloorToInt(timeToDisplay / 60);
    float seconds = Mathf.FloorToInt(timeToDisplay % 60);

    PlayerPrefs.SetFloat("Minutes", minutes);
    PlayerPrefs.SetFloat("Seconds", seconds);
    timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);

    float highscoreMins = PlayerPrefs.GetInt("Minutes");
    float highscoreSec = PlayerPrefs.GetInt("Seconds");

    if (minutes > highscoreMins || seconds > highscoreSec)
    {
        PlayerPrefs.SetFloat("Minutes", minutes);
        PlayerPrefs.SetFloat("Seconds", seconds);
    }
    Debug.Log("HighscoreSec : " + highscoreSec);
}
 public void TimerSystemOn()
{
    if (timerIsRunning)
    {
        if (timeRemaining > 0)
        {
            timeRemaining -= Time.deltaTime;
            DisplayTime(timeRemaining);
        }
        else
        {
            if (timeRemaining <= 0)
            {
                Debug.Log("Time has run out!");
                timeRemaining = 0;
                timerIsRunning = false;

                if (!timerIsRunning)
                {
                    TimeOut();
                }
            }
        }
    }

    if (!timerIsRunning)
    {
        ReachFinishLine();
        DisplayTime(timeRemaining);

    }
}

void DisplayTime(float timeToDisplay)
{
    timeToDisplay += 1;

    float minutes = Mathf.FloorToInt(timeToDisplay / 60);
    float seconds = Mathf.FloorToInt(timeToDisplay % 60);

    PlayerPrefs.SetFloat("Minutes", minutes);
    PlayerPrefs.SetFloat("Seconds", seconds);
    timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);

    float highscoreMins = PlayerPrefs.GetFloat("Minutes");
    float highscoreSec = PlayerPrefs.GetFloat("Seconds");

    if (minutes > highscoreMins || seconds > highscoreSec)
    {
        PlayerPrefs.SetFloat("Minutes", minutes);
        PlayerPrefs.SetFloat("Seconds", seconds);
    }
    Debug.Log("HighscoreSec : " + highscoreSec);
}
deleted 2675 characters in body; edited title
Source Link

How todo I save BestTimemy Highest Time for each level on playerprefs?

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

How to save BestTime each level on playerprefs?

public TextMeshProUGUI timerText;
public bool timeIsRunning;
public float timeRemaining = 10f;
public M_GameManager _GameManager;
public int indexlevel;

private void Awake()
{
    indexlevel = PlayerPrefs.GetInt("Level");
}

// Start is called before the first frame update
void Start()
{
    _GameManager = FindObjectOfType<M_GameManager>();
    timeIsRunning = true;
}

// Update is called once per frame
void Update()
{
    setTimer();
}

public void setTimer()
{
    if (timeIsRunning)
    {
        if (timeRemaining > 0)
        {
            timeRemaining -= Time.deltaTime;
            DisplayTimeUI(timeRemaining);
        }
        else
        {
            Debug.Log("Time has run Out !!!");
            timeRemaining = 0;
            timeIsRunning = false;

            if (!timeIsRunning)
            {
                _GameManager.isTimerunning = true;
                _GameManager.TimeOut();
                Debug.Log(timeRemaining);
            }
        }
    }

    if (!timeIsRunning)
    {
        _GameManager.isTimerunning = true;
        _GameManager.TimeOut();
        DisplayTimeUI(timeRemaining);
    }
}

public void DisplayTimeUI(float timeToDisplay)
{
    timeToDisplay++;
    float minutes = Mathf.FloorToInt(timeToDisplay / 60);
    float seconds = Mathf.FloorToInt(timeToDisplay % 60);

    PlayerPrefs.SetFloat("Minutes" + indexlevel, minutes);
    PlayerPrefs.SetFloat("Seconds" + indexlevel, seconds);

    float highscoreMins = PlayerPrefs.GetFloat("Minutes");
    float highscoreSec = PlayerPrefs.GetFloat("Seconds");

    if (minutes > highscoreMins)
    {
        //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 = " + string.Format("{0:00}:{1:00}", PlayerPrefs.GetFloat("Minutes"), PlayerPrefs.GetFloat("Seconds")));
    }
}

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

How do I save my Highest Time for each level on playerprefs?

 public void TimerSystemOn()
{
    if (timerIsRunning)
    {
        if (timeRemaining > 0)
        {
            timeRemaining -= Time.deltaTime;
            DisplayTime(timeRemaining);
        }
        else
        {
            if (timeRemaining <= 0)
            {
                Debug.Log("Time has run out!");
                timeRemaining = 0;
                timerIsRunning = false;

                if (!timerIsRunning)
                {
                    TimeOut();
                }
            }
        }
    }

    if (!timerIsRunning)
    {
        ReachFinishLine();
        DisplayTime(timeRemaining); 

    }
}

void DisplayTime(float timeToDisplay)
{
    timeToDisplay += 1;

    float minutes = Mathf.FloorToInt(timeToDisplay / 60);
    float seconds = Mathf.FloorToInt(timeToDisplay % 60);

    PlayerPrefs.SetFloat("Minutes", minutes);
    PlayerPrefs.SetFloat("Seconds", seconds);
    timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);

    float highscoreMins = PlayerPrefs.GetInt("Minutes");
    float highscoreSec = PlayerPrefs.GetInt("Seconds");

    if (minutes > highscoreMins || seconds > highscoreSec)
    {
        PlayerPrefs.SetFloat("Minutes", minutes);
        PlayerPrefs.SetFloat("Seconds", seconds);
    }
    Debug.Log("HighscoreSec : " + 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

added 554 characters in body
Source Link

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

I want to save it in the bottom text. enter image description here

I want to save it in the bottom text. enter image description here

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

I want to save it in the bottom text. enter image description here

added 1186 characters in body
Source Link
Loading
added 495 characters in body
Source Link
Loading
added 250 characters in body; edited title
Source Link
Loading
added 21 characters in body
Source Link
Loading
Source Link
Loading