The question is answered, right answer was accepted
Simple Timer
I am needing help geting started on creating a basic timer for my game I would like it to start with 60 seconds then count down to 0.
I know its problualy very basic but I have googled it and cant seem to find any for C#.
Answer by GeorgeRigato · Nov 20, 2012 at 08:06 PM
Hi again
using UnityEngine;
using System.Collections;
public class SimpleTimer: MonoBehaviour {
public float targetTime = 60.0f;
Update(){
targetTime -= Time.deltaTime;
if (targetTime <= 0.0f)
{
timerEnded();
}
}
void timerEnded()
{
//do your stuff here.
}
}
Have fun.
Thx man! But, i have a simple problem... I need to show the time in some gui text or something like this... Can you help me? :)
Answer by drearyplane · Oct 26, 2015 at 12:34 PM
I am using the exact same code for my latest project, but it isn't working. I am in Unity 5.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerMover : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
public float targetTime = 10.0f ;
private Rigidbody rb;
private int count;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
winText.text = "";
}
// Update is called once per frame
void Update () {
targetTime -= Time.deltaTime;
if (targetTime <= 0.0f) {
timerEnded();
}
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.CompareTag ("Pick up")) {
other.gameObject.SetActive (false);
count = count + 1;
SetCountText();
}
if (count >= 10) {
winText.text="You won!";
Application.Quit();
}
}
void FixedUpdate() {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
void SetCountText (){
countText.text = "Memories: " + count.ToString ();
}
void timerEnded (){
winText.text = "You Lost!";
Application.Quit();
}
}
I have put the entire code just in case there is a problem somewhere else.
You haven't said what is happening when you run your code.
I would start by adding some logging code to check (for example) whether or not timerEnded() is being called.
And if you're testing in the editor, note that Application.Quit() will have no effect there. So your code would set the text to "You lost" (once per frame once the timer has reached zero) but carry on.
It's a bit late by now (only two years late) but I did get this to work and I'm getting better at using these places after getting suspended from StackOverflowExchange halfway through a project with a deadline. Now it's quite obvious I should've used Debug.Log. I was very inexperienced at Unity back then.
Answer by Democre · Nov 20, 2012 at 08:46 PM
If you want it to countdown, you could try something like this:
public class Countdown: MonoBehaviour {
public int duration = 60;
public int timeRemaining;
public bool isCountingDown = false;
public void Begin()
{
if (!isCountingDown) {
isCountingDown = true;
timeRemaining = duration;
Invoke ( "_tick", 1f );
}
}
private _tick() {
timeRemaining--;
if(timeRemaining > 0) {
Invoke ( "_tick", 1f );
} else {
isCountingDown = false;
}
}
}
This will give you a public "timeRemaining" member that you can watch or display in another script. It also gives you a flag to watch to determine whether this object is actively counting down.