Hi, please help. I’ve been having trouble with this problem for far too long.
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;
[SerializeField]
Text countdownText;
public AudioSource froggerDied;
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 ;
Debug.Log("currentTime = " + currentTime);
if (currentTime > 5)
{
//this line of code makes the countdown timer turn to a green font
//indicating the player still has time left
countdownText.GetComponent<Text>().color = Color.green;
}
if (currentTime < 5)
{
//this line of code makes the countdown timer turn to a red font
//indicating the player is running out of time
countdownText.GetComponent<Text>().color = Color.red;
}
if (currentTime <= 0)
{
player.transform.position = restartPosition;
PlayerMovement.froggerLives -= 1;
//currentTime = startingTime; I realized this shouldn't be here. Imagine it's gone.
froggerDied.Play(); //this is an audio clip
}
if (restartTime == true) //restartTime is called true in another script but this shouldn't be a problem. This is called when the player hits an obstacle, so the number of lives goes down, and thus restartTime is called in the other script to be true.
{
currentTime = startingTime;
restartTime = false;
}
}
}
Please, don’t just paste the code, it’s hard to read. Instead use the inset code option on the top bar, that way we can better understand what you’re trying to do.
The way it is written, as soon as currentTime reaches 0f it should reset and start counting down again, even if you never set the restartTime bool to true. I suspect you are doing something in another script to disable this script or the GameObject it is attached to if you’re not seeing the timer just repeatedly restart, or you are getting runtime errors somewhere you aren’t mentioning.
For example, what are you doing here? You’re getting the Text component of a Text component. Doesn’t make sense. If this is producing a runtime error, then none of the code which follows is ever going to run.
Hi, this is what the other script looks like. It’s the player Movement script so that’s why it’s lengthy. The part that matters is in the update function, where the Frogger lives are counted.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerMovement : MonoBehaviour
{
[SerializeField]
public float movementSpeed;
public Collider2D playerCollider;
public Vector2 newPosition;
Vector2 restartPosition = new Vector2(0f, -9.25f);
public int froggerLives = 3;
public GameObject froggerLife2;
public GameObject lastfroggerLife;
public GameObject gameoverSign;
public AudioSource characteraudioSource;
public AudioClip froggerJump;
public AudioClip froggerSquash;
public AudioClip froggerDrown;
public AudioClip froggerHurry;
public CountdownTimer CountdownTimer;
public GameObject frogWin1;
public GameObject frogWin2;
public GameObject frogWin3;
public GameObject frogWin4;
public GameObject frogWin5;
public bool Win1;
public bool Win2;
public bool Win3;
public bool Win4;
public bool Win5;
public GameObject player;
void Start()
{
playerCollider = GetComponent<Collider2D>();
Win1 = false;
Win2 = false;
Win3 = false;
Win4 = false;
Win5 = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
newPosition = (Vector2)gameObject.transform.position
+
Vector2.left * movementSpeed;
transform.position = newPosition;
characteraudioSource.PlayOneShot(froggerJump);
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
newPosition = (Vector2)gameObject.transform.position
+
Vector2.right * movementSpeed;
transform.position = newPosition;
characteraudioSource.PlayOneShot(froggerJump);
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
newPosition = (Vector2)gameObject.transform.position
+
Vector2.up * movementSpeed;
transform.position = newPosition;
characteraudioSource.PlayOneShot(froggerJump);
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
newPosition = (Vector2)gameObject.transform.position
+
Vector2.down * movementSpeed;
transform.position = newPosition;
characteraudioSource.PlayOneShot(froggerJump);
}
//This counts the number of lives ------------------ this is where restartTime is called
if (froggerLives == 2)
{
CountdownTimer.restartTime = true;
froggerLife2.SetActive(false);
}
if (froggerLives == 1)
{
CountdownTimer.restartTime = true;
lastfroggerLife.SetActive(false);
}
if (froggerLives == 0)
{
gameoverSign.SetActive(true);
///////////////////////////////////end game (isn't implemented yet)
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Car" || collision.gameObject.tag == "EndZoneGrass")
{
transform.position = restartPosition;
characteraudioSource.PlayOneShot(froggerSquash);
froggerLives -= 1;
}
if (collision.gameObject.tag == "Log" || collision.gameObject.tag == "Turtle")
{
player.transform.parent = collision.gameObject.transform;
}
if (collision.gameObject.tag == "Water")
{
transform.position = restartPosition;
characteraudioSource.PlayOneShot(froggerDrown);
froggerLives -= 1;
}
if (collision.gameObject.tag == "Win")
{
frogWin1.SetActive(true);
transform.position = restartPosition;
Win1 = true;
}
if (collision.gameObject.tag == "Win2")
{
frogWin2.SetActive(true);
transform.position = restartPosition;
Win2 = true;
}
if (collision.gameObject.tag == "Win3")
{
frogWin3.SetActive(true);
transform.position = restartPosition;
Win3 = true;
}
if (collision.gameObject.tag == "Win4")
{
frogWin4.SetActive(true);
transform.position = restartPosition;
Win4 = true;
}
if (collision.gameObject.tag == "Win5")
{
frogWin5.SetActive(true);
transform.position = restartPosition;
Win5 = true;
}
if (Win1 == true && Win2 == true && Win3 == true && Win4 == true && Win5 == true)
{
//nothing is implemented here yet
}
}
void OnTriggerExit2D(Collider2D otherCollision)
{
player.transform.parent = null;
}
}
All that line of code does is change the font. Here are some images of the font changing. Look at the bottom where it says TIME. The code seems to work so that’s why I have it. I don’t believe this is a problem though.
Here is a picture of the console. So, once currentTime = startingTime is called on line 56, then the countdown timer stops working and it stays on 29 forever.