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?