Question by
WillC21 · Feb 28, 2018 at 02:19 PM ·
scripting problemprogrammingtimer
Timer reset on object pickup script?
I currently have a script that makes a timer count down as the player plays through the game, but I want them to be able to pick up medicine and reset the timer?
Any help? This is what I have attached to my HUD.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class CountdownTimer : MonoBehaviour
{
public string levelToLoad;
private float timer = 60f;
private Text timerSeconds;
public GameObject objToDestroy;
// Use this for initialization
void Start()
{
timerSeconds = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
timerSeconds.text = timer.ToString("f1");
if (timer <= 0)
{
SceneManager.LoadScene(levelToLoad);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == ("Player"))
{
Destroy(objToDestroy);
timer = Time.timeSinceLevelLoad;
}
}
}
Comment
Answer by Cuttlas-U · Mar 01, 2018 at 09:33 AM
hey; create another float variable and in the "Start" function make its value to your timer like this :
private float StartTimer = 60f;
void Start()
{
StartTimer = timer;
}
this way u have a variable that u can reset your timer any time u want like this :
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == ("Player"))
{
Destroy(objToDestroy);
timer = StartTimer;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612153632im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Renderer.materials not working in void update() 2 Answers
Change the Intensity of a Haptic Feedback with respect to time ? 0 Answers
How do i solve this? CS1061 1 Answer
Score isn't able to be counted 0 Answers
JsonUtility not working 1 Answer