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?
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