- Home /
Ontrigger not working
Hi so I have an ontriggerfunction that is to stop the timer for 3 seconds and resume it. The timer works and everything I tested it out but nothing happens with my ontrigger function when I hit the object. I set the object as 'is trigger' and added RB to it and all and also added the Pick Up layer to it as shown in my script. Just need some help thanks.
using 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;
private void Start()
{
startTime = Time.time;
cooldownTimer = cooldown;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
Destroy(other.gameObject);
AddCooldown();
}
}
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;
}
}
That should work, but have you really assigned the Tag? Is it spelled correctly (lowwer/upper case)
Another issue has arisen though, the object dissapears but my addcooldown function isnt being called
Try to debug it with Debug.Log("Function called in Trigger"); and Debug.Log("Cooldown Function");
Just did the debug.log without the destroy object in there and it seems that it should be working its co$$anonymous$$g up on console.
Your answer
