- Home /
Timer variable set to zero
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?
I 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)
Answer by KellyThomas · Dec 21, 2013 at 07:20 AM
Time.time
is readonly.
If you want to track the time since the level started the set a variable to hold the value Time.time
returns when gameplay starts.
You can then calculate the difference between this variable and Time.time
to determine how much time has passed since.
Thanks! under this line if(coinsCollected > 9) how can I add that public float LastTime would then change to the value of the time that has passed? and how can I then change under the same line that BestTime has to change to the value of the time that has passed if the time that has passed is greater than BestTime?
Here, this should help get your started:
private float startTime;
public float levelTime {
get { return Time.time - startTime; }
}
void Start() {
startTime = Time.time;
}
void Update() {
Debug.Log(levelTime);
}
Answer by MitzeMitchell · Dec 21, 2013 at 03:34 AM
Try setting myTimer/Time.time equal to 0 in the Start function. That way each time this script is called (i.e. whenever you start the level), it will reset the time.
Side note, in your OnGUI function you have several IfThen statements. The Then statements need to be in {}'s. Technically, it will still compile correctly, but sometimes it will count later If's as they were nested within the first.
Hope this helps!
Please DO NOT post duplicate questions/answers. As per the notification on the top of your screen when you post it, your questions/answers have to go through a moderation queue before they are public.
The only thing you do by posting things multiple times is piss of the mods why look at the queue (in this case me)
Your answer
Follow this Question
Related Questions
timer problem 1 Answer
How to reset timer? (C#) 3 Answers
Multiple Cars not working 1 Answer
Resetting a timer after game over 1 Answer
Distribute terrain in zones 3 Answers