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;
}
}
"but it doesn't do what I want", then what does it do? What isn't working the way it's supposed to work? What have you tried to fix it? Why do you think it doesn't work?
– Benproductions1It simply doesn't count at all anymore with this script.
– usenameI can't set the Time.time to something I decide. If I do it I get this error. error CS0200: Property or indexer 'UnityEngine.Time.time' cannot be assigned to (it is read only)
– usename