I also agree that unity should NOT have built - in functions that fire at specific time intervals.
As you’ve proven, you could easily replicate the behaviour yourself.
In your example, you’ve written something that fires every second. Good. Great!
Are you suggesting that unity should have an OnEverySecond() function?
What if i wanted to fire something every two seconds? Or every five?
You can see that if unity were to add functions for every possible time interval, the api would fill up with quite quickly and it would not make a lot of sense.
You could write your own timer class:
public class Timer {
float seconds;
float timeStamp;
public Timer(float seconds)
{
Set(seconds);
}
public void Set(float seconds)
{
this.seconds = seconds;
this.timeStamp = Time.time;
}
public bool ExpireReset() {
if (Time.time >= timeStamp + seconds) {
Set(seconds);
return true;
}
return false;
}
}
Which on its own would be fine, but you could also extend MonoBehaviour so that it fires events at intervals you choose:
public class IntervalBehaviour : MonoBehaviour {
Timer secondTimer;
void Awake()
{
secondTimer = new Timer(1f);
}
void Update() {
if (secondTimer.ExpireReset())
EverySecond();
}
public virtual void EverySecond() {}
}
You could extend your behaviours from this and have a EverySecond function on each of them, that way. OR, for double extra try-hard mode, you could do this:
public class IntervalMessages : IntervalBehaviour {
public override EverySecond() {
gameObject.SendMessage("OnEverySecond", null, SendMessageOptions.DoNotRequireReceiver);
}
}
And voila. We’ve now implemented what you wanted in the first place. Place this last component on any gameObject, and any behaviour it contains with a “void OnEverySecond();” will fire every second.
Sometimes, you’ve got to do the work youself. Unity can’t do EVERYTHING for you.