- Home /
The question is answered, right answer was accepted
Start timer on button press
Hey guys, I have a countdown timer that works but I want it to only start when the player presses the space button. I also want the timer to reset when the player dies. When the player dies he/she re-spawns back at the start of the level but timer does not reset. Could anyone help me? Thanks :)
using UnityEngine; using System.Collections;
public class CountdownTimer : MonoBehaviour {
 public GUIText _TimeText;
 private float timer = 60f;
 public float addTime = 10;
 public GUIStyle customGuiStyle;
 void Start(){
 }
 void OnGUI() {
     int minutes = Mathf.FloorToInt(timer / 60F);
     int seconds = Mathf.FloorToInt(timer - minutes * 60);
     
     string niceTime = string.Format("{0:0}:{1:00}", minutes, seconds);
     
     GUI.color = Color.yellow;
     GUI.Label(new Rect(850,10,250,100), niceTime, customGuiStyle);
 }
 
 
 void Update(){
     timer -= Time.deltaTime;
     
     
     
     if (timer <= 0) {
         Application.LoadLevel("Level 3 Incomplete");
     }
     
     
     
 }
 void OnTriggerEnter(Collider other)
 {
     if(other.gameObject.tag == "PickUp")
     {
         
         
         Destroy(other.gameObject);
         timer += addTime; 
         
         
     }
 }
}
Answer by murkertrer · Feb 19, 2015 at 03:26 PM
Perhaps it is, because you are not telling the thing to reset the timer? Maibe, when
 if (timer <= 0) {
      Application.LoadLevel("Level 3 Incomplete");
  }
You can make the variable have a new definition?
Something like :
timer = Time.deltaTime + timerAmount increment
Thanks I have it resetting now :) Do you know how I would go about starting the timer when pressing the space bar?
      if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
     {
     Do something
     }
Follow this Question
Related Questions
How to reset timer? (C#) 3 Answers
Calling methods 0 Answers
Start timer with trigger 1 Answer
Countdown Through a Label? 1 Answer
Timer doesn't work properly 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                