0

I am trying to use this script to set a timer for someone looting a player however i can't get it to reset after i call it a second time but it stores the original value of time left where as i want it to start fresh each time. i am calling it from another script which enables and disables it on the OnTriggerEnter and OnTriggerExit methods

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Changetext : MonoBehaviour { public float timeLeft = 5; public Text countdownText; public float cash = 0; // Use this for initialization void start() { timeLeft = 5; } void Update() { timeLeft -= Time.deltaTime; countdownText.text = ("Time Left = " + timeLeft); if (timeLeft <= 0) { countdownText.text = "You got the cash"; cash = 1; } } } 
0

1 Answer 1

3

Start() only gets called once in the gameobjects lifetime - so it won't toggle everytime you enable/disable.

You can instead use OnEnable() to reset the timer to 5;

void OnEnable() { timeLeft = 5; } 

(as an FYI, Start() needs a capital 'S')

Sign up to request clarification or add additional context in comments.

1 Comment

You should also consider disabling the script when the time runs out and and enabling the script when OnEnable is called so it Update method won't be called during that time

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.