- Home /
Question by
gjuarez · Jan 23, 2020 at 10:46 PM ·
time.deltatime
Having trouble restarting a countdown timer using time.deltaTime
I'm having trouble restarting a countdown timer back to 30 seconds. It works the first time it counts down to 0 but afterward, when I try to restart it, it doesn't work.
Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class CountdownTimer : MonoBehaviour {
float currentTime;
float startingTime = 30f;
Vector2 restartPosition = new Vector2(0f, -9.25f);
public GameObject player;
public PlayerMovement PlayerMovement;
public AudioSource froggerDied;
[SerializeField]
Text countdownText;
public bool restartTime;
// Start is called before the first frame update
void Start()
{
currentTime = startingTime;
}
// Update is called once per frame
void Update()
{
countdownText.text = currentTime.ToString("0");
currentTime -= Time.deltaTime ;
if (currentTime > 5)
{
countdownText.GetComponent<Text>().color = Color.green;
}
if (currentTime < 5)
{
countdownText.GetComponent<Text>().color = Color.red;
}
if (currentTime <= 0)
{
player.transform.position = restartPosition;
PlayerMovement.froggerLives -= 1;
currentTime = startingTime;
froggerDied.Play();
}
if (restartTime == true)
{
currentTime = startingTime;
restartTime = false;
}
}
}
Comment
Answer by gjuarez · Jan 22, 2020 at 01:41 AM
Here is the question but on the forums, check this out first
Your answer
![](https://koobas.hobune.stream/wayback/20220612221823im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how do i make this to a negative (ShopRespawn < 180)? 1 Answer
Gameobject moving faster at higher framerates? Using Time.deltaTime 2 Answers
Time class == null in Unity 4.3 1 Answer
How to AddForce OverTime? 1 Answer
healthbar countdown 0 Answers