Hi everyone, I wondered if I could get some help. I’m currently struggling to get an on screen countdown to stop running when a button is pressed. The button has a script in which once pressed completes the level. The timer/countdown also has a script. The button works fine but the timer keeps counting down even when the button is pressed. Could I get some help please? I also made a gamemanager thinking it might help but i’m not sure if it’s over complicating things. I’ll post the scripts i’m using next…
This is the timer script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Timer : MonoBehaviour
{
[Header(“Component”)]
public TextMeshProUGUI timerText;
[Header("Timer Setting")]
public float currentTime;
public bool countDown;
[Header("Limit Settings")]
public bool hasLimit;
public float timerLimit;
[Header("Format Settings")]
public bool hasFormat;
public TimerFormats format;
private Dictionary<TimerFormats, string> timeFormats = new Dictionary<TimerFormats, string>();
// Start is called before the first frame update
void Start()
{
timeFormats.Add(TimerFormats.Whole, "0");
timeFormats.Add(TimerFormats.TenthDecimal, "0.0");
timeFormats.Add(TimerFormats.HundrethsDecimal, "0.00");
}
// Update is called once per frame
void Update()
{
currentTime = countDown ? currentTime -= Time.deltaTime : currentTime += Time.deltaTime;
if (hasLimit && ((countDown && currentTime <= timerLimit) || (!countDown && currentTime >= timerLimit)))
{
currentTime = timerLimit;
SetTimerText();
timerText.color = Color.white;
if (countDown && currentTime <= 0)
{
// call the fail event
Debug.Log("Timer reached 0. Fail event called.");
}
else
{
// call the level complete event
Debug.Log("Timer reached the limit. Level complete event called.");
AddRemainingTimeToNextLevel();
}
enabled = false;
}
SetTimerText();
// if the countdown has less than 10 seconds remaining, change the text color to red
if (currentTime < 10)
{
timerText.color = Color.red;
}
}
private void SetTimerText()
{
timerText.text = hasFormat ? currentTime.ToString(timeFormats[format]) : currentTime.ToString();
}
private void AddRemainingTimeToNextLevel()
{
// add the remaining time on the timer to the next level's total time
float remainingTime = timerLimit - currentTime;
Debug.Log("Adding remaining time of " + remainingTime + " to the next level's total time.");
}
public void StopTimer()
{
enabled = false;
}
}
public enum TimerFormats
{
Whole,
TenthDecimal,
HundrethsDecimal
}
This is the button controller script:
public class ButtonController : MonoBehaviour
{
public Timer timer; // reference to the Timer script
public GameObject timerScriptObject;
public float moveSpeed = 1.0f; // Adjustable speed for the button’s movement
public float moveDistance = 0.05f; // Distance the button should move
public bool isClickable = false; // Flag to track if the button is clickable
private bool isMoving = false; // Flag to track if the button is currently moving
private Vector3 startPosition; // Store the button's starting position
private Vector3 endPosition; // Store the button's ending position
void Start()
{
// Save the button's starting position
startPosition = transform.position;
// Calculate the button's ending position
endPosition = startPosition + Vector3.up * moveDistance;
GameObject timeManager = new GameObject("TimeManager");
timeManager.AddComponent<Timer>();
timer = timeManager.GetComponent<Timer>();
}
void Update()
{
if (isMoving)
{
// Move the button towards the ending position
transform.position = Vector3.MoveTowards(transform.position, endPosition, moveSpeed * Time.deltaTime);
// Check if the button has reached the ending position
if (transform.position == endPosition)
{
// Stop moving the button
isMoving = false;
// Make the button clickable
isClickable = true;
}
}
}
void OnCollisionExit(Collision collision)
{
// Check if the button is no longer colliding with any objects
if (collision.contacts.Length == 0)
{
// Check if the button is not already moving
if (!isMoving)
{
// Start moving the button
isMoving = true;
}
}
}
void OnMouseDown()
{
if (isClickable)
{
// Finish the current level when the button is clicked on
Debug.Log("Level complete!");
timer.StopTimer();
}
}
}
this is the game manager script:
public class GameManager : MonoBehaviour
{
public Timer timer; // reference to the Timer script
public ButtonController buttonController; // reference to the ButtonController script
public GameObject timerObject; // reference to the GameObject that the Timer script is attached to
public GameObject buttonObject; // reference to the GameObject that the ButtonController script is attached to
void Start()
{
// Find the GameObjects that the Timer and ButtonController scripts are attached to
timerObject = GameObject.Find("TimeManager");
buttonObject = GameObject.Find("Button");
// Get the Timer and ButtonController scripts from the GameObjects
timer = timerObject.GetComponent<Timer>();
buttonController = buttonObject.GetComponent<ButtonController>();
// Set the Timer script to be enabled
timer.enabled = true;
}
void Update()
{
// Check if the Timer script is disabled
if (!timer.enabled)
{
// If the Timer script is disabled, disable the ButtonController script as well
buttonController.enabled = false;
}
}
}
Could I get some help please?