Question by
amer-palo · Aug 15, 2021 at 12:23 PM ·
c#unityeditorontriggerenter
Cooldown function not triggered ontrigger
So I have a cooldown function for a timer, where the timer stops for 3 seconds when something happens. I tested it and it works fine with a button trigger however I am trying to do an ontrigger function to allow it to trigger when hitting an object. The trigger should work as I also have a destroy object code on the trigger and it works however the cooldown function isnt working please help.
This a re-post btw since I didn't get a solution when I last posted this.
sing System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro;
public class Timer : MonoBehaviour { [Header("Timer in m:s")] [Tooltip("Time elapsed since game start")] [SerializeField] private string timeElapsed;
[Header("Cooldown in s")]
[Tooltip("Time in seconds for which the timer stops counting")]
[SerializeField] private float cooldown = 3f;
public TMP_Text timerText;
public float startTime;
private float cooldownTimer;
public GameObject clock;
private void Start()
{
startTime = Time.time;
cooldownTimer = cooldown;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
AddCooldown();
Debug.Log("Cooldown Function");
Destroy(other.gameObject);
Debug.Log("Function called in Trigger");
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.V))
{
AddCooldown();
}
cooldownTimer += Time.deltaTime;
if (cooldownTimer >= cooldown)
{
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
}
}
private void AddCooldown()
{
cooldownTimer = 0f;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613041706im_/https://answers.unity.com/themes/thub/images/avi.jpg)