Execution for Every x Seconds
It's quite common to execute functions for every period of time.
And the following is the method I mostly used:
void Start () { nextTime = Time.time; interval = 1; // every single second } void Update () { if (Time.time > nextTime) { nextTime += interval; // do things here } }
Especially, notice the difference of initialization of nextTime
// this would fail when you reload the scene. nextTime = 0; // this would execute in the beginning nextTime = Time.time; // this would execute after an interval nextTime = Time.time + interval;