- Home /
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!!
$$anonymous$$ake it simple:
count -= Time.deltaTime;
int counter = (int)count;
c.text = counter.ToString();
hello, i've tried this code/// it only subtracts one time... i want it to be like timer...
Well, you need to create a function that gets started when you press the button
var runnung:boolean = false;
function Timer()
{
while(count > 0){
count -= Time.deltaTime;
int counter = (int)count;
c.text = counter.ToString();
yield;
}
running = false;
}
if(Physics.Raycast(ray, hit)){
if(hit.transform.name == "hint"){
if(!running){
Timer();
running =true;
}
}
}
Answer by -hiTo- · Sep 03, 2013 at 04:56 PM
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;
}
}
}
}
hello... where is the time in (count-time) co$$anonymous$$g from?
I missed that. I renamed the variable to make more sense.
The answer is now updated.
I reordered the code (I put the backgrounds in as private member variables off the class, and initiated them in Start), so that your needs are met better.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113519im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Timer Countdown not subtracting value. 1 Answer
Double subtraction issue 1 Answer
subtracting numbers gives a very wrong answer 0 Answers
Can anyone help me figure this script out? (Light Trigger) 1 Answer
Timer not work :( 0 Answers