Game Clock (Like in "The Escapist")
Hello guys, I try to make a game clock (Like in the game the escapist), but my problem is that I cant do it global. That means I'm having an empty Object called "Time" and a script called "HandleTime.cs". Now my time ist running when the game starts and gets seperated in minutes, hours, days. The problem is, to access this time in another object (to do something with another object when a specific time is reached), I make a new instance of the "HandleTimers" HandleTimers time = new HandleTimers()
. But when I do this another Timer starts over again. How can I let this script run one time and all elements, that try to do something with the time, have the same time?
Answer by Nomenokes · Aug 21, 2017 at 04:13 AM
I'd recommend using static variables, that is, something that is not instantiated. When you make a new HandleTimers(), it does not access the first one but creates a new timer.
public class Timer : MonoBehaviour {
public static float timer=10;//10 second countdown
void Update () {
timer -= Time.deltaTime;//will count down at the same speed even with lag
}
}
When you want to access this, do Timer.timer
Your answer
Follow this Question
Related Questions
Wait time after coroutine's wait seconds is complete 0 Answers
Trouble Navigating Pause Menu.... 0 Answers
Add a using to every script. 1 Answer
Rotation around player is broken, does rotateAround not work correctly in 2D? 1 Answer
How to reset score when game restart,How to reset score on game restart? 0 Answers