Hi there, i’m working on a simple countdown timer that needs to be reset every 30 seconds or every time one of my players completes an action. The timer correctly restarts when the seconds expire but it won’t restart whenever i complete said action. I’m calling the same method (ResetTimer).
Thinking this could be an issue with the Update() function being called and resetting the textbox (and the timer). Is there a way around this? Does anyone have a working countdown script they’d like to share? New to all this. Thanks in advance!
using System;
using System.Collections;
using System.Reflection;
using System.Threading;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DropMe : MonoBehaviour, IDropHandler {
public Sprite red, blue;
DateTime endTime;
TimeSpan roundTimeLeft;
public GameObject timer, debug;
public void OnDrop(PointerEventData data) {
var original = data.pointerDrag;
original.transform.SetParent(this.transform);
original.transform.position = this.transform.position;
if (original.name == "P1S5") {
//DO STUFF
ResetTimer();
}
if (original.name == "P2S4") {
//DO STUFF
ResetTimer();
}
if (original.name == "P1S2") {
//DO STUFF
ResetTimer();
}
}
public GameObject GetOriginalObject(PointerEventData data) {
return (GameObject)data.pointerDrag;
}
private Sprite GetDropSprite(PointerEventData data) {
var originalObj = data.pointerDrag;
if (originalObj == null)
return null;
var dragMe = originalObj.GetComponent<DragMe>();
if (dragMe == null)
return null;
var srcImage = originalObj.GetComponent<Image>();
if (srcImage == null)
return null;
return srcImage.sprite;
}
private void Awake() {
ResetTimer();
}
void Update() {
if (roundTimeLeft.Seconds < 0) {
ResetTimer();
}
roundTimeLeft = endTime.Subtract(DateTime.Now);
timer.GetComponent<Text>().text = (roundTimeLeft.Seconds).ToString();
}
private void ResetTimer() {
endTime = DateTime.Now.AddSeconds(10);
timer.GetComponent<Text>().text = "10";
debug.GetComponent<TextMeshPro>().text = DateTime.Now.ToString();
}
}
I made this countdown timer for someone a couple of days ago, you could you that instead as I think it’s less complex
using System;
using UnityEngine;
public class Countdown : MonoBehaviour
{
public float CountdownTimerInSeconds = 30f;
public float resetTimerToXSeconds = 50f;
public void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
ResetTimer(resetTimerToXSeconds);
}
CountdownTimerInSeconds -= Time.deltaTime;
}
private void OnGUI()
{
GUI.Label(new Rect(10, 10, 150, 50), FloatToTime(CountdownTimerInSeconds));
}
private void ResetTimer(float newTimeInSeconds)
{
CountdownTimerInSeconds = newTimeInSeconds;
}
private string FloatToTime(float seconds)
{
var span = TimeSpan.FromSeconds(seconds);
return string.Format("{0}:{1:00}:{2:00}", span.Minutes, span.Seconds, span.Milliseconds);
}
}
Unfortunately the issue still persists, the timer resets only when the countdown expires.
I’ve tried adding a watch to the text property on the TextMeshPro object that displays the timer and it comes up with:
“The identifier ‘text’ is not in the scope”
Sorry, missed that. From memory, you use SetText for TextMeshPro
so debug.GetComponent<TextMeshPro>().text = DateTime.Now.ToString();
should be debug.GetComponent<TextMeshPro>().SetText(DateTime.Now.ToString());
Doesn’t seem to be working unfortunately 
Thanks for the script thou, it’s much cleaner than mine 
Weirdly, no. It enters the ResetTimer method and executes the instructions but nothing happens
how does your code look now?
using System;
using System.Collections;
using System.Reflection;
using System.Threading;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DropMe : MonoBehaviour, IDropHandler {
public Sprite red, blue;
public float CountdownTimerInSeconds = 30f;
public float resetTimerToXSeconds = 30f;
public GameObject timer, debug;
public void OnDrop(PointerEventData data) {
var original = data.pointerDrag;
original.transform.SetParent(this.transform);
original.transform.position = this.transform.position;
if (original.name == "P1S5") {
//DOSTUFF
ResetTimer();
}
if (original.name == "P2S4") {
//DOSTUFF
ResetTimer();
}
if (original.name == "P1S2") {
//DOSTUFF
ResetTimer();
}
}
void Update() {
if (CountdownTimerInSeconds < 0)
ResetTimer();
CountdownTimerInSeconds -= Time.deltaTime;
timer.GetComponent<TextMeshPro>().SetText(((int)CountdownTimerInSeconds).ToString());
}
public void ResetTimer() {
CountdownTimerInSeconds = 30f;
timer.GetComponent<TextMeshPro>().SetText("10");
debug.GetComponent<TextMeshPro>().SetText(DateTime.Now.ToString());
}
public void Start() {
ResetTimer();
}
}
Add a Debug.Log under all your //DOSTUFF lines or in the ResetTimer function to check it is being called.
Tested it and ResetTimer is being called and the instructions executed but the timer value does still only change when the countdown expires.
The timer should reset on timeout or onDrop, which you do do in code, but you write “10” even though you reset the time to 30.
Seems like the code might not do what you want, but I would think it should at least reset to 30 seconds when you drop any of those 3 objects. Is that not even the case?
Yeah didn’t notice i forgot to change that 10 to 30.
It should definitely reset when i drop those objects but while it is calling ResetTimer and executing everything inside the method, the text on the timer (and the timer counter itself) won’t actually update. Feels like they’re locked.
Worried it could be because before Update() overrides those values while ResetTimer is active (not sure that can even be a thing thou)
That is really strange, then.
And to put your mind at ease, things in Unity run 1 line at a time 
If you’re at 15 seconds, drop something (to reset it to 30), the next Update should not be setting it to 15 - deltaTime
it should be 30 - deltaTime.
Quick question, how many of these scripts do you have in the game?
Only one script attached to a total of 9 GOs
I’m basically simulating a match by updating values when a specific item is dropped
Edit: Yep, it resets when only attaced to one gameobject. Any way to detect drop without attaching the script to the destination?
Okay, wait a moment here… 1 script, but 9 instances? Do you have 9 different text objects?
No, only have 1 timer but it needs to be updated when i drop an item in one of the nine slots. I’ll have to move the timer code outside of this script and figure out how to call it when i need to reset
Okay, 1 timer object but each of these 9 scripts is updating the same text… that is the issue, for sure, then.
try making the countdowntimer variable static 