I am trying to make a script that does this. Whenever the game starts and enters the scene containing the script then the variable myTimer will be set to zero and so will the Time.time so that the game can be played again from the start with the time counting up from zero instead of from where the last game ended. This is the script I have but it doesn’t do what I want.
using UnityEngine;
using System.Collections;
//ATTACH TO MAIN CAMERA, shows your health and coins
public class GUIManager : MonoBehaviour
{
public GUISkin guiSkin; //assign the skin for GUI display
[HideInInspector]
public int coinsCollected;
public float LastTime;
public float BestTime;
public float myTimer;
private int coinsInLevel;
private Health health;
//setup, get how many coins are in this level
void Start()
{
coinsInLevel = GameObject.FindGameObjectsWithTag("Coin").Length;
health = GameObject.FindGameObjectWithTag("Player").GetComponent<Health>();
}
void Update()
{
myTimer = Time.time;
}
//show current health and how many coins you've collected
void OnGUI()
{
GUI.skin = guiSkin;
GUILayout.Space(5f);
if(health)
GUILayout.Label("Time: " + myTimer.ToString("F2"));
if(coinsInLevel > 0)
GUILayout.Label ("Gifts: " + coinsCollected + " / " + coinsInLevel);
if(coinsCollected > 9)
Application.LoadLevel("Menu");
myTimer = 0;
}
}