Timer not subtracting...

Hi! we have a code for timer. it’s a timer for our hint. When we click hint… it has to count 3 to 1. This is our code.

private var count: float;

private var time: float;

var c: TextMesh;



function Start(){

time=0;

count=3;

}



function Update () {

    if(Input.GetMouseButtonDown(0))

    {

    	var hit: RaycastHit;

     	var ray: Ray=Camera.main.ScreenPointToRay (Input.mousePosition);

     	

     	var background: GameObject;

		background = GameObject.Find("Background");

		var background2: GameObject;

		background2 = GameObject.Find("Background2");

       if(Physics.Raycast(ray, hit))

       {

	       	if(hit.transform.name == "hint")

	         {

	         	time += Time.deltaTime * 1;

				var counter = Mathf.Round(count - time);

				c.text = counter.ToString();

				

		     	background.renderer.enabled= false;

		     	background2.renderer.enabled= true;

		     	if(counter == 0){

		     		background.renderer.enabled= true;

		     		background2.renderer.enabled= false;

		     	}

	         }

       }

    }

}

please help us. The time’s not subtracting. Thank you!!

First off, you are never subtracting anything. You are adding.

time += Time.deltaTime * 1;
(Why * 1? Something * 1 is itself…?);

Second; you are only doing it while you are clicking something (you are only doing it while
Input.GetMouseButtonDown(0) is true).

My suggestion is to replace the time += Time.deltaTime * 1. And you should end up with this:

    private var count: float;     
    private var remainingTime : float;
    private var background: GameObject;
    private var background2: GameObject;
    var c: TextMesh;
     
    function Start()
    {
        background = GameObject.Find("Background");
        background2 = GameObject.Find("Background2");
        remainingTime =0;     
        count=3;     
    }

    function Update () 
    {
        if (remainingTime >= 0)
        {
            remainingTime -= Time.deltaTime;
            var counter = Mathf.Round(remainingTime);     
            c.text = counter.ToString();
        }
        else
        {
            //The Timer Has Finished.
            background.renderer.enabled= true;
            background2.renderer.enabled= false;
        }
     
        if(Input.GetMouseButtonDown(0))     
        {     
            var hit: RaycastHit;     
            var ray: Ray=Camera.main.ScreenPointToRay (Input.mousePosition);
     
            if(Physics.Raycast(ray, hit))     
            {     
                if(hit.transform.name == "hint")     
                {     
                    remainingTime = count;  // Replace time += Time.deltaTime with this

                    background.renderer.enabled= false;
                    background2.renderer.enabled= true;
                }
            }
        }
    }