Mark.S
Mark.S

Reputation: 165

unity c# countdown timer for dynamic times

I've worked out how to make a countdown timer. I would like to make a timer that counts minutes and seconds that I specify in the inspector. when I click an object the timer gets reduced by a few minutes and/or seconds. I will decide a little later. below is my code. Thanks!

using UnityEngine;
using System.Collections;

public class controllerScript : MonoBehaviour {

    public GUIText timerText;
    public float minutes;


    void Start(){
        timerText.text = "";
    }

    void Update(){
        if (Input.GetMouseButtonDown(0)){
            Debug.Log("pressed left click, casting ray");
            CastRay();
        }

        minutes -= Time.deltaTime;
        timerText.text = minutes.ToString("f0") + "";
    }

    void CastRay(){
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity);

        //start statements for what happens when Objects are clicked
        if (hit.collider.gameObject.name == "target01"){
            Debug.Log("you've click obj 1, good work.");

        }
        if (hit.collider.gameObject.name == "target02"){
            Debug.Log("well that's obj 2, even better!");
        }
    }

}

Upvotes: 0

Views: 831

Answers (1)

Dirk Schlenke
Dirk Schlenke

Reputation: 66

Dont know, what exact is your question, but you should realize, that Time.deltaTime is time in seconds and not in minutes.

So you should change the line

minutes -= Time.deltaTime;

to

minutes -= Time.deltaTime / 60.0;

Upvotes: 3

Related Questions