- Home /
How to save score for survival time?
ok so i need help on displaying the current score and the highscore on gameover, i have the timer already set but theres one problem when the game is restarted the timer continues instead of resetting. Im almost done with my game but this part is holding me back.
Timer code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Timer : MonoBehaviour
{
public Text counterText;
public float seconds, minutes;
// Use this for initialization
void Start()
{
counterText = GetComponent<Text>()as Text;
}
// Update is called once per frame
void Update()
{
minutes = (int) (Time.time / 60f);
seconds = (int)(Time.time % 60f);
counterText.text = minutes.ToString("00") + ":" + seconds.ToString("00");
}
}
also this is my script to restart scene and display gameover text, i set time.time = 0f to stop the game from running in background when gameover text is displayed
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public GameObject gameOverText, restartButton;
// Use this for initialization
void Start()
{
gameOverText.SetActive(false);
restartButton.SetActive(false);
}
// Update is called once per frame
void Update()
{ }
void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.tag.Equals("Enemy"))
{
Time.timeScale = 0f;
gameOverText.SetActive(true);
restartButton.SetActive(true);
gameObject.SetActive(false);
}
}
}
Answer by unity_ek98vnTRplGj8Q · Jan 02, 2020 at 03:43 PM
Time.time
will just display the time since application start. Try just making your own timer, and reset it manually on game restart.
void Update(){
myTimer += Time.deltaTime;
minutes = (int) (myTimer / 60f);
seconds = (int)(myTimer % 60f);
}
void RestartTimer(){
myTimer = 0;
}
Your answer
Follow this Question
Related Questions
How to make a scoreboard? 0 Answers
Scoring System 3 Answers
How to increase score by one per second when you are holding an object 2 Answers