- Home /
Problem with timer in Update function
I have a script which assigns a random number at the beginning of the scene, I have a few if statements to assign a value in seconds to the timer depending on what the number is. Here is some code:
using UnityEngine;
using System.Collections;
public class numberOfTaps : MonoBehaviour
{
private float timer;
private bool ranOnce = false;
private int tapCounter = 0;
private int number = 0;
void Start()
{
//Generate a random number
number = Random.Range (50, 250);
}
void Update ()
{
randomTapping();
}
void randomTapping()
{
Debug.Log (number);
if(Input.GetButtonDown ("Fire1"))
{
tapCounter += 1;
}
//Checks to see if the tap count matches the number and turns the object red
if(tapCounter == number)
{
renderer.material.color = Color.red;
}
//Debug.Log (tapCounter);
//Sets the timer according to the number given
if(number >= 50 && number <= 75)
{
timer = 10.0f;
}
else if(number >= 76 && number <= 125)
{
timer = 20.0f;
}
else if(number >= 126 && number <= 175)
{
timer = 30.0f;
}
else if(number >= 176 && number <= 250)
{
timer = 45.0f;
}
//Count down the timer in seconds
timer -= Time.deltaTime;
Debug.Log(timer);
//Destroy the object if the timer reaches zero
if(timer <= 0)
{
Destroy(gameObject);
}
}
//Print out relevant information on screen
void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 20), "Number of taps: " + tapCounter);
GUI.Label (new Rect (10, 30, 100, 20), "RandomNumber: " + number);
GUI.Label(new Rect(10, 50, 100, 20), "Time left: " + timer);
}
}
This is in the update function, i can get my random number alright but the timer never counts down as it keeps restarting, is there a way i can get timer to count to zero before it reaches the end of the code block?
"can i get timer to count to zero before it reaches the end of the code block?" you can do that with a do while loop, but this will not be a timer countdown anymore. could you provide more code so we can help accordind to your actual problem?
Sure, i'll update my question with all my code Edit: Code has been updated, problem is the timer never goes below 44 and then loops back to 45 when the code is executed again
Answer by Exbow · Jul 27, 2014 at 07:24 PM
//Sets the timer according to the number given
if(number >= 50 && number <= 75)
{
timer = 10.0f;
}
else if(number >= 76 && number <= 125)
{
timer = 20.0f;
}
else if(number >= 126 && number <= 175)
{
timer = 30.0f;
}
else if(number >= 176 && number <= 250)
{
timer = 45.0f;
}
Why dont you just reposition this to the start function so you set the timer once then?
Well the random number will be how many times the user will need to click the screen. The timer will be given based on what number comes up as obviously the more clicks needed, the more time that will be needed
well since you need to asign the timer once. place this part of the script after the number = Random.Range (50, 250); on the start function and it will stop reassigning the timer.
Thank you so much, i never even thought of that(trying to over-complicate it ha) Thanks again